Valency and chemical formula
Max's comment sums it up. Beware, I'm gonna have to use many simplifications in order to explain this.
A bit of introduction
The species you mentioned are ionic compounds. For your level it's useful to take ionic bonds as bonds between atoms that have such different electronegativities (ChemGuide typically provides useful introductions, I recommend clicking on that link and reading it carefully) that we can imagine them as different ions. Hence, this bond is the attraction between charged particles. What does that mean? It means that one of the species will have a positive charge and the other a negative charge.
Electronegativity increases in diagonally, as we move from bottom left to top right.
$\hspace{5ex}$
$\hspace{30ex}$ Electronegativity (Periodic Trend) – Source
For now just don't apply the electronegativity concept to noble gases. If you really want to know how their electronegativities are determined, take a look at interesting posts like this.
The more electronegative the atom, the less the chance that it'll bond by losing its electrons. Thus, we can conclude two important rules that occasionally become useful, while they're not highly accurate at higher levels:

Metals in general, and especially alkali and alkali earth metals, will bond ionically by losing their valence electrons.
Non-metals in general, and especially halogens, oxygen and nitrogen, will bond ionically by gaining electrons.


I repeat: The two bullet points above are just rules of thumb. Don't expect them to work all the time. But they're good starting points, so I included them in the answer. Don't hesitate to resort back to the first sentence whenever you ran into cases where you don't get a definitive answer by them.

Lithium oxide's case
So let's apply what we learnt to the $\ce{Li2O}$'s case. Lithium will lose its electrons in order to gain the configuration of a noble gases, in which case it would be helium.
$$\ce{Li(s)-&gt;Li(g)}^{~1}$$
$$\ce{Li(g)-&gt;Li+(g)}$$
Oxygen, however, doesn't favor losing its electrons at all. It will, instead, gain electrons and its electronic configuration would arbitrarily look like that of neon, a noble gas.
$$\ce{O2(g)-&gt;2O^2- (g)}$$
Finally, the two ions will form a lattice. Billions of billions of these ions form $\ce{Li2O}$.
$$\ce{Li+ (g) + O^2- (g) -&gt; Li2O(s)}$$
Hence the overall reaction is
$$\ce{2Li (s) + O2 (g) -&gt; 2Li2O(s)}$$
So let's review what we understood: In lithium oxide, oxygen will gain electrons while lithium will lose electrons, since their electronegativities differ greatly. So we should see how many electrons it will lose in order to get to the configuration of the previous noble gas in the periodic table, while oxygen will gain electrons to reach the configuration of the next noble gas in the periodic table. See this Q/A to gain more insight on how to figure this out using their electronic configuration or group number if you don't already know how.
Note that the formula for ionic compounds is the smallest relation between its ions. By $\ce{Li2O}$ we indicate that for every two lithium ions, one oxygen ion is present in the lattice, crudely, and assuming the crystal is without defect. Think of it like this: There was a need for two lithium ions to provide a single oxygen ion with the electrons it needed to fulfill its stable electronic configuration.
Silver hydroxide's case
Let's solve the case of silver and hydroxide.
The hydroxide ion has a charge of $-1$, hence the trouble of determining the charge it will have while bonded ionicallly with silver is gone! Unfortunately, for now, the best way to find the charge out is memorizing it. You may get some fuzzy speculations by drawing Lewis structures, but poly-atomic ions are sometimes exotic chemical species and unpredictable, since many effects are at play to determine the charge of the ion. You can find a list of them here, or more just with a simple Google search.
You have no choice but to memorize the charge of transition metals though, since they tend to assume abnormal electronic configurations. Over time, you'll face it so much you'll learn by heart that $\ce{Ag+}$ is silver's most stable ion.
Now try to apply the logic and reasoning we used back in $\ce{Li2O}$'s case. What would the formula be?

 $\ce{AgOH}$

Useful tips:

Don't forget, that if the poly-atomic ions are more than one in the ionic formula, you should indicate their number with them in parenthesis.
$\Longrightarrow \rm\color{red}{Wrong:} \quad \ce{(NH4)F,~Ca3PO42}$
$\Longrightarrow \rm\color{green}{Correct:} \quad \ce{NH4F,~Ca3(PO4)2}$
Beware of poly-atomic ions that only consist of one atom. These make for the biggest traps and confusions of the students. Good examples are peroxide, superoxide, and azide. Hence $\ce{Li2O2}$ and $\ce{LiO2}$ are correct! Simply because, the ions in them are not the oxide ion, $\ce{O^2-}$, but $\ce{O2^2-}$ and $\ce{O2^-}$, respectively. 2
The oxidation states of the elements, if mentioned, give clue as to what charge their corresponding ion will have. $\ce{Pb(II) \Longrightarrow Pb^2+ ,~Fe(II) \Longrightarrow Fe^2+ ,~Fe(III) \Longrightarrow Fe^3+}$ and so on.


1: It's simplified if I recall correctly, but we take the lattice as formed by ions in the gas state. It helps in defining lattice enthalpy, and I haven't read anywhere that it's not what's really happening.
2: Actually, you can't necessarily make any ionic compounds with any ions you want. Some of them are too unstable to reasonably exist, like lithium superoxide. But it seems the examiners want you to be a magician anyway.