There is a change in the classical electrostatic energy associated with the rearranged charge distribution, which others have described above. But there is also a purely quantum-mechanical effect, which is basically just a particle-in-a-box effect. The electrons' wavefunctions spread out to encompass both atoms, increasing their wavelengths and reducing their kinetic energies. Roughly, you're doubling the wavelength along one axis. Suppose the original kinetic energy corresponding to the wavelength of each electron along each of the three axes is K. Then the separated atoms have total kinetic energy 6K. If you treat the atom as a particle in a box, with a length of 0.1 nm, then you get K=38 eV. When the two atoms form a molecule, the wavelengths parallel to the bond axis are both roughly doubled. This reduces two of the K terms to K/4, giving a total kinetic energy of about 4.5K. This makes the binding energy 1.5K=57 eV. The actual binding energy is 15.43 eV. So the particle-in-a-box argument is crude, but it does give a result on the right order of magnitude to be an important contribution to the total binding energy.