Atomic diagrams illustrating the filling of the outer electron shells. Image from W.H. Freeman and Sinauer Associates, used by permission. Ionic bonds are formed when atoms become ions by gaining or losing electrons. Chlorine is in a group of elements having seven electrons in their outer shells. Members of this group tend to gain one electron, acquiring a charge of -1.
Sodium is in another group with elements having one electron in their outer shells.
Such bonds are relatively weak, and tend to disassociate in water, producing solutions that have both Na and Cl ions. Formation of a crystal of sodium chloride. The overall effect is electrical neutrality. Image from W.H. Freeman and Sinauer Associates, used by permission. Covalent bonds form when atoms share electrons.