A substance floats if it is less dense, or has less mass per unit volume, than other components in a mixture. For example, if you toss a handful of rocks into a bucket of water, the rocks, which are dense compared to the water, will sink. The water, which is less dense than the rocks, will float. Basically, the rocks push the water out of the way, or displace it. For an object to be able to float, it has to displace a weight of fluid equal to its own weight. Water reaches its maximum density at 4°C (40°F). As it cools further and freezes into ice, it actually becomes less dense. On the other hand, most substances are most dense in their solid (frozen) state than in their liquid state. Water is different because of hydrogen bonding.
A water molecule is made from one oxygen atom and two hydrogen atoms, strongly joined to each other with covalent bonds. Water molecules are also attracted to each other by weaker chemical bonds (hydrogen bonds) between the positively-charged hydrogen atoms and the negatively-charged oxygen atoms of neighboring water molecules. As water cools below 4°C, the hydrogen bonds adjust to hold the negatively charged oxygen atoms apart. This produces a crystal lattice, which is commonly known as 'ice'.
Ice floats because it is about 9% less dense than liquid water. In other words, ice takes up about 9% more space than water, so a liter of ice weighs less than a liter water. The heavier water displaces the lighter ice, so ice floats to the top. One consequence of this is that lakes and rivers freeze from top to bottom, allowing fish to survive even when the surface of a lake has frozen over. If ice sank, the water would be displaced to the top and exposed to the colder temperature, forcing rivers and lakes to fill with ice and freeze solid.