One magnesium atom loses two electrons, to become a +2 ion (cation).
Two fluorine atoms gain one electron each to become two 1 ions (anions).
These are held together with an ionic bond. Voila, magnesium fluoride, MgF2.
Check me out: http://www.chemistnate.com