Hydrogen is unusual: it has only one electron and no inner shell. When it bonds to a strongly electronegative atom such as oxygen, the shared pair is pulled toward the oxygen and the hydrogen nucleus is left almost bare, a small, concentrated δ+. A lone pair on the oxygen of a neighbouring molecule can come very close to that exposed proton. This close, strong attraction, written O–H···O, is a hydrogen bond. Only N, O, and F are electronegative enough, and small enough, to produce it.
Its effect shows clearly in boiling points. Down group 16, the hydrides H₂Te (−2 °C), H₂Se (−41 °C), and H₂S (−60 °C) boil lower as the molecules get smaller, the usual dispersion trend. Continue that line and H₂O should boil somewhere near −80 °C; it actually boils at 100 °C. Hydrogen bonding accounts for the gap. In ice, each molecule hydrogen bonds to four neighbours in an open hexagonal lattice that takes up more room than the liquid, so ice is about 8% less dense than water and floats.
Not every molecule with hydrogen qualifies. Methane, CH₄, has four hydrogens, but carbon barely pulls on them, so none is exposed and no hydrogen bond forms. HCl is polar, but chlorine is too large and diffuse to make a strong hydrogen bond, so HCl has ordinary dipole-dipole forces. And a hydrogen bond is not a covalent bond: in water it is worth roughly 20 kJ/mol, against about 460 kJ/mol for the O–H bond inside the molecule. The test is structural: an H on N, O, or F, facing a lone pair on N, O, or F.
Think of it like this. A hydrogen on oxygen is like a magnet with its plastic cover peeled off: the bare positive end can press right up against a neighbour's lone pair, so it grips far harder than ordinary polar sticking. A hydrogen on carbon keeps its cover on and barely sticks. Even the bare magnet still holds much more weakly than the covalent bond inside the molecule.
Core idea. Hydrogen bonded to N, O, or F is left as an exposed δ+ that is strongly attracted to a lone pair on another N, O, or F, which gives small molecules such as water unusually strong attractions to each other.