Most scientists agree that the atmosphere and the ocean accumulated gradually over millions and millions of years with the continual 'degassing' of the Earth's interior.
According to this theory, the ocean formed from the escape of water vapor and other gases from the molten rocks of the Earth to the atmosphere surrounding the cooling planet.
After the Earth's surface had cooled to a temperature below the boiling point of water, rain began to fall—and continued to fall for centuries. As the water drained into the great hollows in the Earth's surface, the primeval ocean came into existence. The forces of gravity prevented the water from leaving the planet.