You could think about it in terms of how everything forms. You start off with:
A magnesium atom with 2 e- in its outer shell - wanting to get rid of those two e-.
Two oxygen atoms, each with six - wanting to gain two.
Two hydrogen atoms, each with one - wanting to get gain (or lose) one to fill (or empty) its outer shell.
There are two options, 1xMg + 1xO -> MgO. Where Mg loses 2xe- and O gains 2xe- and everyone has a full/empty outer shell. This does happen.
Here though, we have Mg(OH)2 forming.
Think of it in two steps:
Each H shares 1xe- with each O. Now, O has seven (six of its own, plus one shared from H) and H has two (one of its own, plus one shared from O). So now you have two lots of OH, both of which have an O with seven e-, both of which are wanting to gain 1xe- each.
Then consider the Mg, with 2xe- that it wants to give away.
So Mg gives 1xe- to each OH forming Mg2+ + 2xOH-
BUT, possibly you're asking why does Mg and OH have ionic bonds, rather than covalent bond. If so...
H covalently bonds to O as described above - H fills its shell and O gets closer to filling its shell (6xe- -> 7xe-)
If Mg formed a covalent bond, it would go from 2xe- to 4xe- (since it would form two shared pairs of e-). In which case, Mg is further away from full/empty outer shell than before it formed its bonds. By forming ionic bonds, it empties its outer shell (2e- -> 0xe-).
As a teacher in an international school and a marker of A-level papers, I would advise against taking an English exam board paper until your English improves considerably. It is an unfortunate fact that no matter how good your chemistry knowledge is, if you cannot understand the question or cannot explain your answer you will not score well on an English paper.