Magnesium fluoride (MgF2) is not a molecular/covalent compound; magnesium has a low electronegativity (it is a metal, after all) and fluorine has a high electronegativity (it is a non-metal, halogen, and has the highest electronegativity of all atoms on the table). So, a transfer of electrons occurs.
Magnesium, when it bonds to a non-metal, always loses two electron to become a +2 ion. This is because the neutral atom has two valence electrons in its outer shell, and to satisfy the octet rule, it wants to lose those two electrons. This makes it a cation.
Fluorine atoms, on the other hand, carry seven valence electrons in the outer shell of their neutral atoms. So their preference is to gain one electron each, and get a -1 charge; this negatively-charged particle is called an anion.
We require one magnesium atom, giving away its two electrons, giving one electron each to two fluorine atoms:
And you can watch this process occur here: