When this reaction occurs, strong ionic bonds form between two oppositely charged particles (ions).
Magnesium is a group II metal, and therefore has two electrons in it's highest energy level (or outermost electron shell). When the reaction with oxygen occurs, these two electrons are donated by the magnesium, forming positively charged Mg2+ ions.
The two electrons donated from one magnesium atom are taken up by an oxygen atom. Oxygen atoms have 6 electrons in their highest energy level, and require a further 2 to fill the energy level. Therefore, they readily take up these two electrons to form negatively charged O2- ions.
Now that two oppositely charged ions have formed, strong electrostatic attraction forms between them and magnesium oxide is formed in a giant ionic lattice structure. This means that strong ionic bonds occur between the magnesium and oxygen ions in all directions.