You should read up on Proust's law, better known as the Law of Definite Proportions. This is a chemical law that defines your question more generally, on why the ratio of elements and ions are always fixed.
Basically, this compound Magnesium(II) Chloride is MgCl2 because it has the same number of protons, neutrons, and electrons all the way. This defines the properties of the compound or atom.