Magnesium atoms lose two electrons from its outer shell, thus form positvely charged ions with a charge of 2+. These 2 electrons are transferred to and gained by a diatomic molecule of chlorine atoms - each atom acquiring an electron to its outer shell. These oppositely charged ions in ionic compounds are held together by strong electrostatic forces of attraction, which we call ionic bonds. A lot of energy is needed to overcome these forces in order to separate the ions ad cause the substance to melt. This is why ionic compounds must be heated to high temperatures before they change state - thus they have high melting points. These magnesium ions have more than one charge (2+). These highly charged ions will attract other ions more strongly than ions with one charge, therefore more energy will be needed to overcome the electrostatic forces of attraction and so the melting points will be higher. These strong ionic bonds allow "billions" of ions to be packed together in a regular repeating arrangement called a lattice structure.