You’ll remember that earlier, we learned that atoms tend “want” an outermost shell. And we learned that one way of accomplishing that is by donating or gaining electron(s) from other atoms, which results in the formation of ions and ionic bonds.
Another way atoms can get full outermost shells is by sharing electrons. When atoms share one or more pairs of electrons with each other, a covalent bond is formed.
When two or more atoms chemically bond, a molecule is formed. In other words, a molecule is formed when two atoms join (chemically bond) to each other.
Let’s illustrate how covalent bonds are formed by looking the formation of a single covalent bond in a hydrogen molecule (H2), and double covalent bonds in molecules of oxygen (O2) and carbon dioxide (CO2).
The electron shell of hydrogen atoms contains one electron but they “want” to have a full shell. The first shell holds a maximum of two electrons. So, if two hydrogen atoms get together, they can share a pair of electrons (one from each atom). Part of the time, the two electrons spin around one of the hydrogen atoms and part of the time they spin around the other atoms. This conveys the idea of sharing. When you share something with someone, part of the time they have the object and part of the time, you have the object.
Oxygen atoms have six electrons in their outermost shell but they the outermost shell can hold eight electrons. Two oxygen atoms can form a molecule of oxygen by sharing two pairs of electrons.
Carbon dioxide consists of one carbon atom and two oxygen atoms. Each of the two oxygen atoms bond to the carbon atom by means of a double covalent bond.
A carbon atom has four electrons in its outermost shell. In order to get eight electrons in its outermost shell, it can share two pairs of electrons with two oxygen atoms. Each of the oxygen atoms have six electrons in their outermost shell. They can get eight electrons in the outermost shell by sharing two pairs of electrons.
If you wish, you can watch this video that reiterates how covalent and ionic bonds form.