How do covalent bonds work?
When two atoms form a covalent bond, their total energy is reduced by their proximity. It thus takes energy to separate them. If that energy isn’t available, they will cling to one another indefinitely. The two ways in which they lower their total energy by being close are (1) electrostatic attraction and repulsion and (2) lower kinetic energy. Two atoms experience both attractive and repulsive forces as they approach one another. Their positively charged nuclei repel one another, their negatively charged electrons repel one another, but their nuclei attract their electrons. The nuclei never get very close and the electrons manage to stay relatively far apart, too. The dominant effect is an attraction between the electrons and the two nuclei. The result is a net attraction. The nearby atoms are pulled toward one another by these electric forces. The lower kinetic energy comes about because of quantum effects. The electrons travel about the nuclei as waves. When the atoms are far apart, the electrons must orbit their individual atoms. Because they are then confined to small domains, they must have short wavelengths. These waves must be short enough to fit properly into their small confines. Short wavelength objects have high kinetic energies (e.g. short wavelength light is x-rays and gamma rays). But when the atoms are touching, the electrons can spread out between both atoms. Their wavelengths increase and their kinetic energies diminish. These two effects (lowered electrostatic potential energy and lowered kinetic energy) reduce the total energy when the two atoms touch. The result is the covalent bond.