Because the strength of their intermolecular forces varies.
Halogens are located in group 17 of the periodic table and, with the possible exception of astatine,
These diatomic molecules are formed when two halogen atoms come together to form a covalent bond. Because the two atoms that form said bond are identical, the bonding electrons will be shared equally.
This implies that both the bond, and more importantly the diatomic molecule itself, will be nonpolar.
States of matter are actually determined by the strength of the intermolecular forces associated with a substance's molecules. Stronger intermolecular forces are characteristic of solids and liquids, while weaker intermolecular forces are characteristic of gases.
Here is where the difference between the halogens is made.
London dispersion forces are formed when random variations in a molecule's electron cloud arise. These variations distort the electron cloud of a molecule, creating an uneven distribution of charge and giving rise to partial positive and partial negative charges.
The larger the electron cloud of a molecule is, the more significant these distortions will be. In turn, this will cause the strength of the London dispersion forces to increase.
Larger molecules have larger electron clouds, so you can expect the halogen with the larger diatomic molecule to exhibit the strongest London dispersion forces.
This is why, at room temperature, iodine, which has the largest molecule, is a solid, bromine, which is the next in line in terms of size, is a liquid, and fluorine and chlorine, the smallest of the molecules, are both gases.
So remember, nonpolar molecules only exhibit weak London dispersion forces.
These forces depend on the size of the molecule's electron cloud, which in turn depends on the size of the molecule itself.
As a result, larger molecules will have stronger London dispersoon forces and possibly be solids or liquids, while smaller molecules will have weaker London dispersion forces and be gases.