Let's start by taking a look at the 'bare' oxygen atom. While simplistic depictions of atoms often show the electrons orbiting the nucleus on various circular orbits as if they were planets, quantum mechanics predicts something somewhat different. Instead of imagining electrons as tiny planets on fixed orbits, it is better to think of them as standing waves. These waves have energies which can be calculated and the predicted result is that:
- the lowest energy standing wave is essentially a sphere
- the second-lowest energy standing wave is a larger sphere but with a spherical node inside
- the third-lowest energy standing waves are a set of three which look a little bit like dumbbells. These all have a planar node passing through the nucleus and the two parts on opposite sides of the nodal plane have different phases.
If you have dabbled a bit in chemistry, you may recognise these shapes as s and p orbitals.
In another result that directly derives from ab initio quantum mechanical calculations, the electrons have a weird property called spin. We don't need to understand what exactly spin is but the consequence of this property is that one single standing wave will always be 'occupied' by two electrons. If we now fill in oxygen's eight electrons into these waves, we can put two in the lowest energy wave (a 1s orbital), two in the second-lowest (a 2s) and the remaining four in the third-highest waves (the three 2p orbitals; one of these will be twice populated, the other two only contain a single electron).
Now to add hydrogen atoms. Hydrogen only has one of these orbitals occupied by its single electron: the 1s orbital. But how do we describe the bond that is being formed here? Well, quantum chemistry has answers to that too. As two atoms move closer, their standing waves overlap; and as waves do they can either overlap in constructive interference or destructive interference. For mathematical reasons, if we are putting two waves (orbitals) together like this, we need to have a result that also contains two waves (orbitals), which is why there will always be a bonding (constructive interference) and an antibonding orbital (destructive interference; a nodal plain vertically between the atoms, perpendicular to the bond axis).
If we first take a look at only a single hydrogen atom, this could approach the oxygen atom and thus by extension one of the p orbitals in different ways as shown in the image below. (Due to symmetry, only an angle between 0° and 90° makes sense; phase designation is arbitrary.)
At 0°, the possible overlap between the two orbitals and thus the extent of constructive/destructive interference is greatest. That means, if we add the two orbitals together as outlined above, the resulting orbital will have the lowest possible total energy and it will be most favourable to the system. At 45°, the overlap is less good, but still okay. At 90° we have a problem: whatever we may gain from constructive overlap on one side of the p orbital's nodal plane (the top half in the picture) we will lose from destructive overlap on the other side. These two sides mathematically cancel each other out so that overall there is zero energy gained by this type of bonding.
Having said that, I should return to the p orbitals themselves. As you may already know or have guessed, these can be thought of as 'pointing' in three different directions with 90° angles between them; much like the axes of a three-dimensional coordinate system. Therefore, if a hydrogen orbital maxes out the energy gain with one of these p orbitals, it will necessarily have zero overlap with the other two.
Nevertheless, this arrangement of the hydrogen atoms forming a 90° angle, each perfectly overlapping with exactly one of the half-occupied p orbitals is energetically the initially most favourable state. In the absence of other forces, this is what we should expect. Quite unlike what gandalf has suggested, tetrahedral arrangement is not favourable a priori, as it would require raising the energy of the two electrons in the s orbital in a process called hybridisation. The energy gained from lowering the energy levels of the p orbitals when forming sp3 hybrid orbitals is necessarily less than the energy lost when raising the s electrons to that same energy level. This is why larger central atoms like sulphur (92° bond angle in H2S), selenium (91° in H2Se) and tellurium (90° in H2Te) have bond angles very close to 90°.
It should, at this point, be obvious why linear is not an option: it would require both hydrogen orbitals to interact with the same p orbital on oxygen. This isn't impossible per se (cf. compounds such as XeF2); however, it is energetically very unfavourable in the case of two hydrogen atoms and one oxygen atom.
Nonetheless, we still have to explain why the angle is actually 104.5° – a pretty large deviation from the expected 90°. In a nutshell, this is because oxygen is very small and the bonds between oxygen and hydrogen are short. Thus, assuming a 90° bond angle the hydrogen atoms would be very close together and their nuclei repulse each other. This repulsion is slightly stronger (and thus, loses more energy) than the gain by a perfect bond angle. Therefore, the oxygen atom undergoes partial hybridisation to increase the bond angle. In this process, the two p orbitals which would form the bond each receive a contribution of the s orbital. Ultimately, instead of two dumbbells and a sphere the three orbitals will end up looking like two deformed dumbbells and a deformed sphere. (The third p orbital will remain unaffected as it is perpendicular to the two O–H bonds.) The sweet spot where the energy gain from reduced repulsion can no longer overcome the energy lost from introducing hybridisation is at approximately 104.5°, corresponding to the experimentally determined bond angle of water.
Finally note that the angle is not rigid. Slight deviations in the bond angle only cause a small increase in overall energy. In fact, all water molecules at more than a couple of kelvin above absolute zero are vibrating to some extent; one of the vibration modes involves increasing and decreasing the angle. So the 104.5° is, in actual fact, only the average and energetically most favourable outcome.