Which temperature is meant in the Gibbs free energy equation?
If you write $\Delta G = \Delta H -T\Delta S$, you implicitly admit that temperature is constant and is the same for a non-isolated system and its environment.

You basically assume that the system and the environment are in thermal equilibrium, at a given temperature $T$. Thermal equilibrium means that when system loses heat, environment absorbs it: this must occur equally in both directions, otherwise a temperature gradient would settle between system and environment. We could describe thermal equilibrium as the result of "a zero balance of rate of heat transfer between system and environment". That's what goes on during a phase transition (e.g. solid to liquid, where entropy of the system increases due to heat given by the environment, whereas temperature stays constant).

Now, applying the second law of thermodynamics to an ensemble system + environment, undergoing an infinitesimal transformation, we write:

$$\mathrm dS_\text{syst} + \mathrm dS_\text{env} \geq 0 \tag1$$

As said above, the entropy of the environment changes if there's a  reversible heat exchange with the system:

$$\mathrm dS_\text{env} = \frac{\delta Q_{env}}{T}=-\frac{\delta Q_{syst}}{T} \tag2$$

Merging eq. $\text{(1)}$ and $\text{(2)}$, you write:

$$\mathrm dS_\text{syst} - \frac{\delta Q_{syst}}{T} \geq 0 \tag3$$

Now the first law allows you to write:

$$\delta Q - P\mathrm dV_\text{syst}=\mathrm dE_\text{syst} \tag4$$

and then

$$T\mathrm dS_\text{syst} - \left(\mathrm dE_\text{syst}+P\mathrm dV_\text{syst}\right) \geq 0  \tag5$$

Now (and this is crucial), assuming both pressure and temperature constant, you end up with

$$\mathrm d{\left(E_\text{syst}+PV_\text{syst}-TS_\text{syst}\right)_{T,P}} \leq 0 \tag6$$

The expression within the brackets is a state function of the only system and corresponds to the free energy. Quoting Wikipedia:


  A state function describes the equilibrium state of a system. For example, internal energy, enthalpy, and entropy are state quantities because they describe quantitatively an equilibrium state of a thermodynamic system, irrespective of how the system arrived in that state.


Considering that $E+PV=H$, we write:

$$\left(\mathrm dG_\text{syst}\right)_{T,P} = \left(\mathrm dH_\text{syst}-T\mathrm dS_\text{syst}\right)_{T,P} \leq 0 \tag7$$

Now, while equation $\text{(1)}$ is much more general, equation $\text{(7)}$ applies only to the situation where the work is expansion or contraction and for constant temperature and pression. Nonetheless, since for most situations of chemical interest, those conditions are satisfied, eq $\text{(7)}$ maintains a well deserved relevance and is very convenient to establish whether a process is spontaneous or not, within the limitation of constant temperature and pressure.



A chemical reaction occurs down to the point where $\left(\Delta G\right)_{T, P}=0$.

For a general reaction:

$$\ce{$a$A + $b$B &lt;=&gt; $l$L + $m$M}$$

For constant temperature and pressure:

$$\left(\Delta G\right)_{T, P}=l\mu_{\ce L} + m\mu_{\ce M} - a\mu_{\ce A} - b\mu_{\ce B}  \tag8$$

being $\mu$ the chemical potential for each of the species involved.
The chemical potential relates to activity, according to:

$$\mu=\mu^\circ+RT\ln a \tag9$$

Equation $\text{(8)}$ and $\text{(9)}$ shows that free energy varies with the composition (expressed as activity) of our system: that's exactly what happens during a chemical reaction.

Proceeding further, you will end up with Van't Hoff isotherm:

$$\left(\Delta G\right)_{T, P}=\left(\Delta G^\circ\right)_{T}+RT\ln\frac{a_{\ce L}^{l}a_{\ce M}^{m}}{a_{\ce A}^{a}a_{\ce B}^{b}}$$

Activities vary until a chemical equilibrium is established, so that $\left(\Delta G\right)_{T, P}=0$.