We do not know exactly how the Earth came to have so much water. It is everywhere in the Universe, but rarely in such quantity. Reasoning from first principles, every element except hydrogen has been formed in stars. Therefore oxygen was originally formed in stars. The formation of water is not a problem: it is exothermic, so forming the molecule from its atoms does not need outside energy. But to explain how the Earth has so much in comparison with, for example, Mars, is not easy. It is an undecided problem in planetary geology.