Galaxy groups are not scaled down versions of massive galaxy clusters – the hot gas in groups [known as the intragroup medium (IGrM)] is, on average, less dense than the intracluster medium, implying that one or more non-gravitational processes (e.g. radiative cooling, star formation and/or feedback) has had a relatively larger effect on groups. In the present study, we compare a number of cosmological hydrodynamic simulations that form part of the OverWhelmingly Large Simulations project to isolate and quantify the effects of cooling and feedback from supernovae (SNe) and active galactic nuclei (AGN) on the gas. This is achieved by comparing Lagrangian thermal histories of the gas in the different runs, which were all started from identical initial conditions. While radiative cooling, star formation and SN feedback are all necessary ingredients, only runs that also include AGN feedback are able to successfully reproduce the optical and X-ray properties of groups and low-mass clusters. We isolate how, when and exactly what gas is heated by AGN. Interestingly, we find that the gas that constitutes the present-day IGrM is that which was not strongly heated by AGN. Instead, the low median density/high median entropy of the gas in present-day groups is achieved by the ejection of lower entropy gas from low-mass progenitor galaxies at high redshift (primarily 2 ≲z≲ 4). This corresponds to the epoch when supermassive black holes accreted most of their mass, typically at a rate that is close to the Eddington limit (i.e. when the black holes are in a ‘quasar mode’).