The Gibbs energy has not been derived this way. It came from a thought process. About 100 years ago, Gibbs was puzzled by the very existence of spontaneous endothermic reactions. In mechanics, objects always fall down. They never "fall up" spontaneously. They never gets more energy spontaneously. In exothermic reaction, the matter looses spontaneously energy, like a falling object. In endothermic reactions, the atoms and molecules receive energy from the surroundings. They increase their energy. It is surprising. So Internal energy U and Enthalpy H are not equivalent to the gravitational energy. A spontaneous reaction can happen in chemistry with $\Delta$H <$0$ ou >$0$. As a consequence, H or U are not the good criteria to foresee whether a chemical reaction will spontaneously happen.
So Gibbs was thinking : What is the energy that always gets out of a system of molecules and atoms in a spontaneous reaction, even when the reaction is endothermic ? The answer is of course : the electric energy. A galvanic cell can only produce energy, whatever its heat effet. A working galvanic cell may become hotter or cooler, but it can only deliver electric energy. The amount of electric energy delivered by a cell is defined by the difference of a new potential energy, called Gibbs energy $G$, and defined so that $\Delta$$G$ is always negative in a cell. $\Delta$$G$ is obtained form the expression : $\Delta$$G$ = - $zEF$, where $E$ is the voltage of the cell, $F$ is the Faraday, and $z$ is the number of electrons exchanged in the reaction inside the cell. Furthermore, $\Delta$$G$ is defined and calculated in the same way as $\Delta$$H$. Gibbs energies of formation exist like enthalpies of formation.
For example, one may write : $\Delta$$G_{react} = \Sigma$$G_{fin}$-$\Sigma$$G_{in}$ and so on. You probably know the rest to the theory. And for example the relationship between Gibbs energy and the equilibrium constant.
Now if you plot $\Delta$$H$ and $\Delta$$G$ of a given chemical reaction versus $T$. you will notice that $\Delta$$H$ practically does not change much with $T$. But the voltage $E$ and of course $\Delta$$G$ do change much with $T$. The line $H$ vs. $T$ is nearly horizontal. But the line $G$ vs $T$ is inclined. The strange result of this drawing is the fact that both lines are joining one another at $T$ = $0$ $K$. So the difference of these energies is of course : $H-G$ = $T\Delta S$, where $\Delta S$ is the slope of the $\Delta G$ curve.