When a covalent bond is formed, attraction-repulsion stabilisation occurs. The molecule will want to be in a 'conformation' where the energy is at a minimum so as to ensure highest stability. Stability is affected by the attraction-repulsion forces and the distance of separation between the atoms. I drew a graph which roughly shows how energy would change as we change the separation between atoms:
As we increase the distance between atom such as O2, energy approaches zero (charges far apart therefore less energy). As we decrease the distance between the atoms (that is bringing them closer), energy increases due to increased interactions between the charged particles (or the oxygen atoms). There will be a specific distance between the atoms where energy is at a minimum, as shown in the diagram above.