* Magnesium (Mg) has 2 valence electrons. It tends to lose these electrons to achieve a stable, full outer shell like the noble gases. This results in a +2 charge, forming the magnesium ion (Mg²⁺).
* Oxygen (O) has 6 valence electrons. It tends to gain 2 electrons to achieve a stable, full outer shell. This results in a -2 charge, forming the oxide ion (O²⁻).
Since magnesium readily loses electrons and oxygen readily gains them, they are attracted to each other due to their opposite charges. This electrostatic attraction between the positively charged magnesium ion and the negatively charged oxide ion forms the ionic bond.
The resulting compound is magnesium oxide (MgO).