When the earth coalesced out of the primordial gas cloud that surrounded the young sun, hydrogen and oxygen were two of the chemical elements incorporated in the earth. Hydrogen is of course by far the most common element everywhere in the universe: you can't
form a planet without hydrogen. Oxygen is less abundant in the universe, but it is a common element too. Oxygen and hydrogen are chemically quite reactive, and so they both combined with various other elements to form chemical compounds. One of these compounds is water, and water quickly became an
important part of the earth's surficial layer, along with many other familiar chemicals (silica - a major component of sand, calcium carbonate - what limestone is made of, etc.). A significant amount of earth's water has probably been delivered by comets, especially in the first billion years or so after earth formed. (With partial help from Mad SciNetwork)