Molecules Are All About Atoms Saving Energy
It can seem mysterious that two or more atoms will somehow "choose" to connect themselves together. After all, each atom is surrounded by a cloud of negative charge and these like charges should mean that atoms repel each other. But yet we know that atoms do come together and bond with each other. They can do this because electrons are more complex than the negatively charged little balls we might first imagine. Even though their charges repel, they can overcome this repulsion by aligning their quantum spins with each other and sharing orbitals. By bonding, atoms can achieve lower overall energy states by improving the stability of their electron configurations. Atoms must still meet each other in order to bond; they must collide. Solvents such as water supply opportunities for atoms and molecules to approach so they can react with each other. This is why water is such an important part of our bodies. Our biochemistry depends on molecules, atoms and ions reacting with each other. Deep in the Earth, heat and pressure improve the chances of atoms meeting each other and bonding. That is how minerals form.
The formation of oxygen gas, common in our atmosphere and essential to us, is a simple example. When heat is applied to a collection of free oxygen atoms, they move faster and tend to collide with each other more often and with greater energy. Out of these collisions some atoms will bond. The rate depends on pressure, temperature and the reactivity of the particular atoms involved. As we learned, oxygen atoms are very reactive. Single oxygen atoms don't last long near the Earth's surface where it's warm and there is some pressure. Here, when any two oxygen atoms become available and collide, they immediately bond into an oxygen molecule, O2. Each atom pairs two valence electrons into a double bond, which is energetically favourable over its single atom energy state, in which the electron configuration isn't quite stable. Neon gas, however, is always found in a single atom state because it is almost completely inert. Its electron configuration is already very stable.
Reactivity is the reason why atoms choose to bond with each other. Reactive atoms are atoms with slightly unstable electron configurations. Atoms, like all other systems, prefer a lowest possible energy state. They can achieve this by attaining a noble gas-like valence octet by attracting or offering electrons to other atoms, stabilizing their octets as well. This is how ionic bonds work. Or they can pair up valence electrons with other atoms by sharing orbitals, achieving a lower overall energy state. This is how covalent bonds work. In reality, most chemical bonds fall into a spectrum somewhere in between these two pure extremes, where some charge separation occurs and some orbital overlap always occurs.
The opportunity to achieve a more stable lower-energy state is why chemical bonding happens. It is made possible because electrons are not too tightly bound to their nuclei. They have some wiggle room to move about in their energy orbitals and they can interact with other atom's electrons. It's also possible because electrons are quantum mechanical particles. They can share energy orbitals with each other, following distinct quantum mechanical rules. All chemical bonds are ultimately a delicate interplay between the charge (repulsive and attractive forces), energy (electron shell), angular momentum (orbital shape) and the spin of electrons between atoms.