The valence of magnesium is two. The valence of an atom is determined by the number of chemical bonds it makes; those chemical bonds are both ionic and covalent. Magnesium is part of the alkaline earth metals and is a divalent metal.
Valence electrons are on the same level, and they have a greater attraction to the nucleus due to this. Because of this greater attraction, the atom grows smaller.
The smallest part of an element that takes part in a chemical reaction is an atom. An element is a substance that does not break down into something that is less simple than itself.