Two hydrogen atoms together form the hydrogen molecule \mathrm{H}_2. The electrons are what bind the two atoms, but with two electrons the repulsion between them has to be handled as well, and the equations get complicated. So we start from the hydrogen molecular ion \mathrm{H}_2^+, the hydrogen molecule with one electron removed. It consists of just two protons and one electron, and it is the simplest molecule there is.
A proton is about 1836 times heavier than an electron. While the electron moves around, the protons hardly move. So we fix the two protons at a distance R from each other and solve only for the state of the electron. This is called the Born-Oppenheimer approximation. Solving again for each value of R gives the energy of the molecule as a function of the distance between the protons.
The electron is attracted by both protons. With r_A and r_B the distances from the two protons, the potential energy of the electron is V = -\dfrac{e^2}{4\pi\varepsilon_0}\left(\dfrac{1}{r_A} + \dfrac{1}{r_B}\right). The lower curve in the figure shows it along the axis through the two protons. It drops steeply at each proton.
When the protons are far apart, the electron is near one of them, in the ordinary 1s orbital of a hydrogen atom, with energy -13.6\,\mathrm{eV}. The state near proton A, \psi_A, and the state near proton B, \psi_B, are two states with the same energy that differ only in where the electron is.
As the protons come closer, the tails of the two 1s orbitals begin to overlap. The shaded area in the upper part of the figure is this overlap. At the same time the potential energy between the protons drops, and once R is shorter than 8a_0, its value at the midpoint falls below the -13.6\,\mathrm{eV} line. Here a_0 = 52.9\,\mathrm{pm} is the Bohr radius. From then on, the electron can no longer be said to belong to one proton or the other.
We write the state of the electron with the protons close together as a superposition of the two 1s orbitals, \psi = c_A\psi_A + c_B\psi_B. The coefficients c_A and c_B are chosen so that the expectation value of the energy is as low as possible. This method is called LCAO, the linear combination of atomic orbitals. It is not the exact solution, but it is enough to show why a bond forms.
The two protons cannot be told apart, so the probability of finding the electron near A must equal the probability of finding it near B. This means |c_A| = |c_B|. With real coefficients, only two choices remain: the sum with the same sign, \psi_+ \propto \psi_A + \psi_B, and the sum with opposite signs, \psi_- \propto \psi_A - \psi_B.
The upper part of the figure shows \psi_+ and the lower part \psi_-. The dashed lines are the two original 1s orbitals and the solid lines are their sum and difference. \psi_+ does not go to zero between the protons, because the tails of the two peaks add up there. \psi_- changes sign at the midpoint and has a node there.
The size of the overlap is measured by the overlap integral S = \int \psi_A\psi_B\,dV. For two 1s orbitals the integral can be done exactly, giving S = e^{-R/a_0}\left(1 + \dfrac{R}{a_0} + \dfrac{R^2}{3a_0^2}\right). At R = 2.5a_0, as in the figure, S = 0.46. To make the total probability of finding the electron somewhere equal to 1, \psi_\pm is multiplied by \dfrac{1}{\sqrt{2(1 \pm S)}}.
Superposing two states gives two states. \psi_+ is called the bonding orbital and \psi_- the antibonding orbital. Neither belongs to a single atom. Both extend over the whole molecule and are called molecular orbitals.
The probability density of finding the electron is the square of the wave function. Squaring \psi_\pm gives |\psi_\pm|^2 = \dfrac{\psi_A^2 + \psi_B^2 \pm 2\psi_A\psi_B}{2(1 \pm S)}, which contains a term \pm 2\psi_A\psi_B that the separate atoms do not have. It is nonzero only where the two orbitals overlap, and it is called the interference term.
The dashed line in the figure is the density \dfrac{\psi_A^2 + \psi_B^2}{2} that the electron would have if it were near A with probability one half and near B with probability one half. In the bonding orbital (top), the density at the midpoint is 37% higher than this, and it is lower at the protons and outside them. In the antibonding orbital (bottom) it is the other way around: the density at the midpoint is zero, and it is higher at the protons and outside them.
When there is electron density between the protons, its negative charge pulls both protons toward the midpoint. The protons repel each other because both are positive, but the pull of the electron between them cancels that repulsion, and the protons stay at a definite distance where the forces balance. This is the covalent bond. The electron is shared by the two atoms.
In the antibonding orbital there is little density between the protons, and the pull toward the midpoint is too weak. The repulsion between the protons is larger, and they move apart. With the electron in the antibonding orbital, the molecule does not stay bound.
Once quantum mechanics has given the electron density, the force on each proton can be calculated with Coulomb's law, treating the density as a distribution of charge. This is the Hellmann-Feynman theorem. In 1939 Feynman showed that every force on the nuclei in a molecule can be obtained in this way. To think of the bond in terms of forces, it is enough to look at where the electron density is.
The energy of the molecule is the energy of the electron plus the repulsion energy between the protons, \dfrac{e^2}{4\pi\varepsilon_0 R}. For the LCAO orbitals all the integrals needed can be written as functions of R, and the result is E_\pm(R) = E_{1s} + \dfrac{e^2}{4\pi\varepsilon_0 R} - \dfrac{J \pm K}{1 \pm S}, where E_{1s} = -13.6\,\mathrm{eV} is the 1s energy of a hydrogen atom.
J is called the Coulomb integral. It is the energy of attraction between proton B and an electron near proton A. K is called the exchange integral, and it is nonzero only when the two orbitals overlap. Its sign in the formula is opposite for \psi_+ and \psi_-, so it lowers the energy of \psi_+ and raises the energy of \psi_-.
The figure plots E_\pm against the distance R between the protons, measured from the energy of \mathrm{H} and \mathrm{H}^+ far apart. The bonding curve has a minimum at R = 132\,\mathrm{pm}, with a depth of 1.76\,\mathrm{eV}. The protons balance at this distance, and it becomes the bond length. The antibonding curve has no minimum. Its energy only rises as the protons come closer.
The energy rises when the protons are pushed too close because the repulsion between them grows rapidly, in proportion to \dfrac{1}{R}. When the protons are far apart, the orbitals hardly overlap and the energy drops very little. When they are close, the repulsion is larger. In between there is a distance where the two balance.
With the protons held fixed, the equation for the hydrogen molecular ion can also be solved exactly. The exact solution gives a bond length of 106\,\mathrm{pm} (2.00a_0) and a depth of 2.79\,\mathrm{eV}, shown as the point marked "Exact solution" in the figure. LCAO makes the bond about 25% too long and the depth about 37% too small, but it correctly shows that the curve has a minimum, that is, that a bond forms.
The measured binding energy of \mathrm{H}_2^+ is 2.65\,\mathrm{eV}, which is 0.14\,\mathrm{eV} less than the depth of the minimum. Even at the bottom of the curve the protons keep their zero-point vibration, so the energy of the molecule lies above the minimum by the zero-point energy.
The figure places the energies of the molecular orbitals beside the 1s levels of the original atoms. From the two 1s levels come a bonding orbital below them and an antibonding orbital above them. The antibonding orbital rises by more than the bonding orbital falls. Since the overlap integral S is not zero, the denominator 1 \pm S in E_\pm is smaller for the antibonding orbital.
In the hydrogen molecular ion \mathrm{H}_2^+, the one electron goes into the bonding orbital. In the hydrogen molecule \mathrm{H}_2, the second electron also goes into the bonding orbital, because the Pauli exclusion principle allows up to two electrons with opposite spins in one orbital.
With both electrons in the bonding orbital, there is more density between the protons, and the bond becomes stronger and shorter. The bond length of \mathrm{H}_2 is 74\,\mathrm{pm} and its binding energy is 4.48\,\mathrm{eV}. Compared with 106\,\mathrm{pm} and 2.65\,\mathrm{eV} for \mathrm{H}_2^+, the energy is 1.7 times larger. It is not twice as large because the two electrons repel each other.
A helium atom has two electrons. If we try to make \mathrm{He}_2 from two helium atoms, there are four electrons, two in the bonding orbital and two in the antibonding orbital. The antibonding orbital rises by more than the bonding orbital falls, so the total energy is higher than that of the two separate atoms and no bond forms. This is why helium exists as a gas of single atoms.
The number of electrons in bonding orbitals minus the number in antibonding orbitals, divided by two, is called the bond order. It is 1 for \mathrm{H}_2, \dfrac{1}{2} for \mathrm{H}_2^+ and 0 for \mathrm{He}_2. Removing one electron from \mathrm{He}_2 gives \mathrm{He}_2^+ with bond order \dfrac{1}{2}, and it has indeed been observed as a bound ion.
The bonds in nitrogen and oxygen molecules can be explained in the same way. The 2p orbitals also form bonding and antibonding orbitals, and putting electrons into them in order of increasing energy gives the strength of the bonds and the properties of the molecules. In \mathrm{O}_2, for example, the last two electrons go into two separate antibonding orbitals with their spins aligned. This is why liquid oxygen is attracted by a magnet.