Some of the water has been on earth since it formed from the cloud of gas and dust around our young sun. This cloud contained hydrogen and oxygen and when the earth formed, some of hydrogen and oxygen reacted together to form water.
The rest of the water came from the many comets that hit the earth in it's first billion years or so of existence (there were more comets around in the solar system at this time because this wasn't too long after the formation of the sun). Comets consist somewhat of frozen water (amongst other chemicals) so when these collided with the earth, more water was introduced to the planet.
Eventually large amounts of water was trapped on the earth, which pooled to form the oceans.