Not entirely sure on the details as it’s been a few years since my chem degree but an abbreviated explanation goes as follows:
The first topic to introduce is how temperature affects the motions of molecules. Molecules are in constant motion, except at absolute zero (0K or -273 celcius) and that motion increases with temperature. So as molecules are heated up, they begin to move faster and faster; they acquire more kinetic energy, which is why materials expand when heated. Conversely, as the temperature is lowered, the motion of these molecules becomes sluggish.
Water consists of two hydrogen molecules bonded to an oxygen molecule. Oxygen is more electronegative (electron attracting) than hydrogen and so pulls the hydrogen’s electrons away from them. This results in the hydrogens having a very slight positive charge, and the oxygen having a slight negative charge, creating a dipole. As we know, like charges repel whilst unlike charges attract and so, when to or more water molecules are brought into close proximity, they will reorient themselvelves so that the opposite ends of dipoles are facing each other. Think of two bar magnets: if you place them close to each other, they’ll naturally orient themselves so that the North Pole of one magnet lines up with the South Pole of the other. The same thing happens with H20, but with an electric dipole as opposed to a magnetic one. We call the force of attraction ‘hydrogen bonding’. For water, it turns out that these hydrogen bonds have an optimal distance of 1.5 to 2.5 Angstroms (1 Angstrom is one ten billionth of a metre). As a result, in scenarios where hydrogen bonding is do,infant water molecules will naturally an intermolecular distance of 1.5-2.5 A
So, to summarise we have the thermal motion of water, and an attractive force due to hydrogen bonding. At temps above 4 degrees, thermal motion is diominant and the molecules ‘ignore’, to a greater or lesser extent, hydrogen bonding. As the temperature drops, this thermal motion becomes more sluggish, and the molecules can begin to pack more closely together. This increases the density until the temperature drops to4 degrees. Below this point, hydrogen bonding forces become significant and start to ‘coerce’ adjacent molecules into adopting that optimal distance of 1.5-2.5 A. This spacing is on average greater than the spacing between water molecules at 4 degrees and hence the molecules ‘spread out’ over a greater distance. More spread out molecules = lower density.
Hope this goes some way to answering your question.