For a closed system in contact with a constant temperature reservoir held at the initial system temperature T, and in contact with a surroundings at an external pressure held at the initial system pressure P, and experiencing a change between two thermodynamic equilibrium states, the change in internal energy is given by: $$\Delta U=Q-W$$If we express the work W as the sum of expansion work and non-expansion work (the latter, say, obtained by generating electricity or turning a shaft), then $$W=P\Delta V+W_{NE}$$And, because there is a single constant temperature bath comprising the surroundings, the heat added to the system is related to the change in entropy between the initial and final thermodynamic equilibrium states of the system by:
$$\Delta S=\frac{Q}{T}+S_{gen}$$where $S_{gen}$ is the entropy generated as a result of any irreversibility's that may be present. So, if we combine these equations, we obtain:$$W_{NE}=-\Delta G-TS_{gen}$$where $\Delta G=\Delta U-T\Delta S+P\Delta V$. So, for any process that goes between the same two thermodynamic equilibrium states, the maximum non-expansion work that the system can do on its surroundings is $-\Delta G$ (since $S_{gen}$ can only be positive). The maximum corresponds to a reversible path between the two end states.