The first molecules of water formed out in space, about 14 billion years ago, as part of the nebula left over after an early star exploded in a supernova. There were a lot of hydrogen and oxygen atoms floating around in these nebulae, and when they stuck together that made water molecules. Because hydrogen and oxygen were both very common atoms, they made a lot of water molecules.
Hydrogen and oxygen atoms stuck together because hydrogen atoms were able to share their electrons with oxygen atoms, and oxygen atoms wanted two more electrons than they had. We call this a covalent bond. When two hydrogen atoms shared their electrons with an oxygen atom, it made a molecule that was stronger than any of the atoms were on their own, so most of the oxygen in space is formed into water molecules.
There is still plenty of water all over space and other planets. There’s water in nebulae today, and in comets, and on the Moon, and on Mercury, Mars, and Neptune, for instance. But the water in all of these places is frozen into ice. Earth is the only place we know of that has liquid water ( possibly Mars does too).
Nobody knows how water got to Earth, or whether it was here from the beginning when the planet first formed, but there has been liquid water on Earth for about four billion years. Water’s an important part of heating and cooling the Earth, and also water is where the first living cells got started. All plants and animals need water to survive.
When water gets too cold, or the pressure gets too high, it turns into ice. When water gets too hot, or the pressure gets too low, it turns into steam. Up in the air, water is what clouds are made of; then it falls as rain, or as snow.