Magnesium is divalent. This means that one magnesium atom needs to lose 2 electrons in order to become stable.
Chlorine, on the other hand, is monovalent. This means that one chlorine atom needs to gain one electron in order to become stable.
Based on this, one magnesium atom will combine with two chlorine atoms, where the magnesium loses two electrons, one for each chlorine.
The formula of the compound formed is: MgCl2