How does the reaction Mg + O₂ → MgO occur?
First of all, do you know that chemical equations are means to express chemical reactions?

Second, this equation represents the reaction of magnesium with oxygen. Magnesium and Oxygen are the reactants and Magnesium Oxide is the product.

The arrow indicates the direction of the reaction.

To write basic chemical equations, you only need to know your reactants, your products and how to write the symbols for them. Usually, you can 'guess' what the products can be from generic patterns or mechanism, but I don't think you will be there yet (or required to remember them) soon if you just got started on them.

To get the molecular formula or ionic formula in this case, you need to know the valencies of the elements constituting that compound. For instance here, Magnesium has valency 2, Oxygen has valency 2, so $\ce{MgO}$ is the formula for Magnesium Oxide.

For example, Sodium Oxide, Sodium has valency 1, Oxygen has valency 2, so $\ce{Na_2O}$ is the formula for Sodium Oxide.

Iron (III) Oxide becomes: $\ce{Fe_2O_3}$ (Iron valency = 3; Oxygen valency = 2)

Magnesium Chloride becomes $\ce{MgCl_2}$ (Magnesium valency = 2; Chlorine valency = 1)

Does this answer your question or were you looking for something different?