Here's why:
* Magnesium (Mg) is a metal and tends to lose two electrons to form a +2 cation (Mg²⁺).
* Sulfur (S) is a nonmetal and tends to gain two electrons to form a -2 anion (S²⁻).
The opposite charges of the resulting ions (Mg²⁺ and S²⁻) will attract each other, forming an ionic bond.