we do not know exactly how the earth came to have so much water. it is everywhere in the universe, but it is uncommon for a place to have so much. 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 why the earth has so much compared to, for example, mars, is not easy. it is an undecided problem in planetary geology.