All these rules are approximate, and all of them follow from Schrödinger's equation that offers a much more accurate, quantitative result.
Bonds in molecules may arise not just from "valence bonds", which remain localized in atoms, but also from "molecular orbitals" which are localized across the whole molecule. The latter kind of bond is more general and the methods to study it are newer.
Back to the valence bonds. Energetically, the bonds occur because they allow the pair of atoms to reduce its energy. Just like for individual atoms, pairs of atoms are more stable when a whole orbital is filled because the other states that could be filled are separated by a gap and they have a higher energy.
So whenever it's possible to divide the electrons among the pair of atoms so that both atoms have full shells, then all the electrons with low enough energy have been added, to minimize the ratio of energy per electron, and the addition of an extra electron would increase the energy by a much higher amount, which suggests that the previous full-shell solution is at least a local minimum of the energy per electron.
Because the interactions between the electrons themselves are complicated, this superficial wording can't really replace a calculation - quantum mechanics for many electrons, which deals with a $3N$-dimensional wave function. In fact, in this picture, even in the Hartree-Fock approximation, many other emergent phenomena occur such as "orbital hybridization" - the emergence of totally new kinds of orbitals that are appropriate to describe the molecule.