Ionic bonding holds oppositely charged ions together in giant structures. Strong electrostatic forces of attraction act in all directions. Each ion in the lattice is surrounded by ions with the opposite charge and so is held firmly in place.
The sodium chloride structure contains equal numbers of sodium and chloride ions as shown by its formulae, NaCl. The sodium ions and chloride ions alternate to form a cubic lattice.
The ratio of ions in the formulae and the structure of an ionic compound depend on the charges on the ions. For example, magnesium ions are Mg2+, and chloride ions are Cl- so the formula of magnesium chloride is MgCl2. Its structure contains twice as many chloride ions as magnesium ions.