Active galactic nucleus (AGN) outflows are the heat given up when gas in a galaxy evolves towards thermodynamic equilibrium. Indeed, while AGN feedback regulates the growth of massive galaxies, its origin can be understood as the spontaneous thermodynamic process which ensures that the (Gibbs) free energy of the system always decreases, enabling the galaxy to reach a more energetically favourable state. In particular, it is shown that feedback heating processes will be favoured whenever the hot atmosphere of a galaxy would effectively gain energy as a result of cooling. For example, as the hot atmosphere of a galaxy cools and contracts, the work done by gravity will be thermalized, with a fraction of the gas also being captured by stars and the supermassive black hole at the centre of the galaxy. If this gain of energy exceeds the loss of energy that occurs when cooling gas drops out of the atmosphere, the Gibbs free energy of the system would increase overall. Since this is energetically unfavourable, feedback heating is initiated which acts to reduce the net cooling rate of the atmosphere, thereby preventing any build-up of energy. The Gibbs free energy can also decrease in the absence of feedback heating, but only if the loss of energy due to mass dropping out of the atmosphere exceeds the gains of energy described above. Therefore, to ensure that the Gibbs free energy always decreases, a galaxy will necessarily flip between these two states, experiencing episodes of heating and cooling. Due to the close long-term balance between heating and cooling, the gas in a galaxy will evolve quasistatically towards thermodynamic equilibrium, which has the observable appearance of galaxy growth being regulated by AGN feedback. The same mechanism also provides an explanation for why strong AGN feedback occurs more frequently in cool-core galaxy clusters than in non-cool-core clusters.