In chemistry, ionic bonds and covalent bonds are both methods atoms use to combine into larger molecules by swapping or sharing outer electrons. An atom's energy level is determined by the number and configuration of electrons orbiting the atomic nucleus. As atoms collide, those with unstable electron configurations form either ionic bonds or covalent bonds with each other in order to reach stable energy levels.
Ionic bonds occur when atoms join together, attracted by ions with an opposite charge. For instance, a common example of an ionic bond is sodium chloride, commonly known as table salt. Here, the sodium atom has a single valence electron above a stable level, which is eight electrons. When it meets a chlorine atom, which has seven valence electrons, the chlorine atom removes the sodium atom's single valence electron. The resulting ionic compound contains two atoms with stable octets of electrons. They include a positively charged sodium ion and a negatively charged chloride ion.
A covalent bond occurs when two atoms join together to share a valence electron in order to achieve stable energy levels. For instance, a common covalent compound is water. An oxygen atom has six valence electrons, so it needs two additional valence electrons in order to achieve a stable outer energy level. Hydrogen atoms each contain one valence electron, so they each need an additional valence electron in order reach a stable configuration. Therefore, an oxygen atom can form a covalent bond with two individual hydrogen atoms, creating the covalent compound water.