Atomic number of Magnesium atom is 12.
Electronic configuration is :
Mg = 2, 8, 2
So it contains 2 valence electron. In order to achieve the nearest noble gas configuration, it loses two electrons to form Magnesium ion.
Mg2+ = 2,8
Atomic number of Chlorine(Cl) atom is 17.
Electronic configuration is :
Cl = 2, 8, 7
So it contains 7 valence electron. In order to achieve the nearest noble gas configuration, it gains one electron to form Chloride ion.
Cl- = 2,8,8
Due to the electrostatic force of attraction an Ionic bond is formed between Magnesium ion and two Chloride ion by complete transfer of one electron to each Chlorine ion and this results in the formation of magnesium chloride.