Hydrogen is the most abundant element in the universe. By a long long way.
Helium is the second most abundant, but it doesn’t really react with anything.
Oxygen is the third most abundant element. This is due to the ways stars die and progressions of nuclear fusion in stars that tend to result in a lot of the smaller elements being “used up” in fusion before the star actually dies. There’s over twice as much oxygen as the next most common element (Carbon).
There’s about 3x as much Carbon as the next element in line, Neon, which like Helium doesn’t really react with anything. Carbon will react with oxygen, but also reacts with other stuff too.
Next up is Iron - again skipping a lot of other smaller elements due to how stars progress through fusion and eventually die. Iron does react with oxygen, but it also reacts with loads of other stuff too. Theres about 10% as much Iron as oxygen.
Next up is Nitrogen, which reacts with Oxygen, it really wants to react with other Nitrogen so a lot of this is taken up without reacting with oxygen.
Then Silicon, Magnesium and Sulphur - and at around 4-6% the abundance of Oxygen.
TL;DR: There’s a lot of oxygen in the universe (compared to everything except hydrogen and helium) and it outnumbers many of the things it reacts with by a large margin, and those things also react with other things, so can’t easily use up all the oxygen.
The end result is that during planetary formation you’re going to end up with a lot of oxygen and hydrogen hanging around together. These immediately react with each other with very little help, to form H2O - AKA water, a very stable compound at temperatures we see on earth.