We present global multidimensional numerical simulations of the plasma that pervades the dark matter haloes of clusters, groups and massive galaxies (the ‘intracluster medium’; ICM). Observations of clusters and groups imply that such haloes are roughly in global thermal equilibrium, with heating balancing cooling when averaged over sufficiently long time- and length-scales; the ICM is, however, very likely to be locally thermally unstable. Using simple observationally motivated heating prescriptions, we show that local thermal instability (TI) can produce a multiphase medium – with ∼ 104 K cold filaments condensing out of the hot ICM – only when the ratio of the TI time-scale in the hot plasma (tTI) to the free-fall time-scale (tff) satisfies tTI/tff≲ 10. This criterion quantitatively explains why cold gas and star formation are preferentially observed in low-entropy clusters and groups. In addition, the interplay among heating, cooling and TI reduces the net cooling rate and the mass accretion rate at small radii by factors of ∼ 100 relative to cooling-flow models. This dramatic reduction is in line with observations. The feedback efficiency required to prevent a cooling flow is ∼ 10−3 for clusters and decreases for lower mass haloes; supernova heating may be energetically sufficient to balance cooling in galactic haloes. We further argue that the ICM self-adjusts so that tTI/tff≳ 10 at all radii. When this criterion is not satisfied, cold filaments condense out of the hot phase and reduce the density of the ICM. These cold filaments can power the black hole and/or stellar feedback required for global thermal balance, which drives tTI/tff≳ 10. In comparison to clusters, groups have central cores with lower densities and larger radii. This can account for the deviations from self-similarity in the X-ray luminosity–temperature () relation. The high-velocity clouds observed in the Galactic halo can be due to local TI producing multiphase gas close to the virial radius if the density of the hot plasma in the Galactic halo is >rsim 10−5 cm−3 at large radii.