Why does the Gibbs free energy need to be minimized for an equilibrium?

Okay, I've found a decent and simple derivation.

Suppose a system is in thermal contact with its environment which we suppose acts as a resevoir of energy - which means it can absorb or release energy without changing its temperature. We call $S_{tot}$ the total entropy of the system + environment, and any variables with subscript $R$ belong to the resevoir while varibles without subscript refer to those of the system.

The idea is that the total entropy $S_{tot}$ will always increase due to the second law of thermodynamics. Let us consider a small change in the entropy

$$dS_{tot} = dS + dS_R >0.$$

Suppose the system and its environment are at the same constant pressure $P$, but the volume of the system can change (and hence so can the volume of the resevoir). If this is the case than we can write the thermodynamic identity

\begin{align} dS_R &= \dfrac{dE_R}{T}+\dfrac{P}{T}dV_R\\ & = -\dfrac{dE}{T}-\dfrac{P}{T}dV, \end{align}

because a change in the energy of the environment will be negative the change of the energy of the system - same reasoning applies to the volume. Now we plug this into the equation above

\begin{align} dS_{tot} & =dS -\dfrac{dE}{T}-\dfrac{P}{T}dV\\ &= \dfrac{-1}{T}(dE +pdV-TdS)\\ &= \dfrac{-dG}{T} >0. \end{align}

It then follows that $dG<0$, and that the system will evolve to one where the Gibbs free energy is minimal, since this is where the total entropy is greatest.

Reference: "An introduction to Thermal Physics" by Daniel V. Schroeder.