Covalent bonds are formed when two or more atoms share electrons. In CO2, each carbon atom shares two electrons with each oxygen atom, forming two covalent bonds.
In contrast, MgBr2, LiCl, and NaCl are all ionic compounds. Ionic compounds are formed when one or more electrons are transferred from one atom to another, creating positively and negatively charged ions. In MgBr2, for example, the magnesium atom loses two electrons to the bromine atoms, creating Mg2+ and 2Br- ions.