Ionic bonds occur between ions that have opposite charges. An ionic bond is the electrostatic force that holds ionic compounds together. When a metal reacts with a non-metal, electrons are transferred from the metal which forms a postive ion to the nonmetal which forms a negative ion. The positive and negatve ions are then attracted to each other.
An example is the reaction between sodium and chlorine. Sodium is a very reactive metal and chlorine is a very reactive nonmetal. Each sodium atom loses an electron to form Na+ and each chlorine atom gains an electron to form Cl-. The resulting sodium and choride ions form a crystal lattice (three-dimenional structure) with a ratio of one Na+ to one Cl-. The formula of an ionic compound always represents the lowest whole-number ratio of ions, but the substance isn't in the form of individual molecules.
The ratio of ions in a formula is related to the charges of the ions. For example, Magnesium chloride has the formula MgCl2. Magnesium loses two electrons to form a +2 ion, so there must be two Cl- ions for every Mg(2+) ion to have a neutral compound.