Sugar crystallizes out of a syrup.
Rock candy is made of large sugar crystals that grow slowly from a supersaturated sugar solution. You dissolve as much sugar as possible in boiling water, usually about two or three cups of sugar per cup of water. Hot water can hold far more dissolved sugar than cold.
As the syrup cools, it holds more sugar than it normally could, so the extra sugar is ready to come out of the solution. When you hang a string or wooden stick in the syrup, the sugar molecules latch onto it and line up into crystals. Rolling the stick in sugar first gives them a head start.
Over about a week, the crystals grow larger as water slowly evaporates. The slower the process, the bigger and clearer the crystals.
Rock candy is a classic kitchen science experiment, and it's also used to sweeten tea in places like Iran and East Frisia in Germany, where large sugar crystals are poured over with hot tea.