The State Function
In what follows, we will restrict our attention to systems (batteries) which are in thermodynamic equilibrium. What this essentially means is that we can actually define the Volume, $V$, Pressure, $p$, Temperature, $T$, and Amount, $n$, of the system. An entire post could be written discussing just what we mean by these terms and what equilibrium means for them, but for our purposes here we’ll take their colloquial meanings.
This severely restricts our attention because we cannot talk about, for example, a battery heating up. Only its equilibrium state before and after some process. Typically we cannot describe the process between two states since the path between these states is in thermodynamic disequilibrium. The thermodynamic variables $p,V,n,T$ are not defined. For more on this point, I recommend the short book Understanding Thermodynamics.
It is assumed that when the battery is in thermodynamic equilibrium, and so the thermodynamic variables $p,V,n,T$ are defined, they are related via some empirical function $f(p,V,n,T) = 0$. For example, in a perfect gas we have $pV = nRT$, where $R$ is the gas constant, a value found empirically. Generally, the form of $f(p,V,n,T)$ will depend on the phase of the substance and the substance itself. The perfect gas form of this function is essential for what follows because the empirical function $f(p,V,n,T) = 0$ is unknown for the majority of substances. We will use the perfect gas equation, $pV = nRT$, as a platform to deviate from.
Internal Energy
A fundamental law of physics, and thermodynamics in particular, is the Conservation of Energy. Feynman has the classic discussion of this, so I will only state it as,
$$\begin{equation} dU = dq + dw\end{equation}$$
where $dU$ is the infinitesimal change in internal energy of the system (all the energy within the battery), $dq$ is the energy exchange between the battery and it’s environment through undirected motion (heat), and $dw$ is the energy exchange between the battery and it’s environment through directed motion (work). The equation is stating, simply, that the only way total energy can change within the battery is through exchanges with the environment. Energy can be neither created nor destroyed. Further, it delineates energy exchange between directed and undirected motion.
Internal energy is a function of the state variables $U = g(p,V,n,T)$ and takes a form based on the system in question. We rarely actually state what this function is because we are more concerned with changes to it due to exchanges with the environment.
It is import to know that functions of state variables are state variables themselves. So, internal energy, $U$, is a state variable. What we want to do now is find a way to express $dq$ and $dw$ as changes in state variables. That way, we can talk about the system using only state variables. We can ignore the environment.
So, define Entropy, $S$, via,
$$\begin{equation} dS := \frac{dq_{rev}}{T}\end{equation}$$
The subscript on $dq_{rev}$ indicates the process of heat exchange is performed reversibly. This is a technical term which we will only come across once more. Basically, a reversible process is an idealisation of reality where at each instant the system is in thermodynamic equilibrium. That way, we can express the path of the process with thermodynamic variables.
Let’s also recognise that work is force times distance. So,
$$\begin{equation}dw = F\times d = \Delta (pV) = p\Delta V + V\Delta p = dw_{exp} + dw_{non-exp}\end{equation}$$
We’ve decomposed work into expansionary and non-expansionary work. Moreover, each of these terms is expressed purely in thermodynamic variables.
We can put these new terms together into the conservation of energy equation to get, for a closed system (no exchange of matter) at constant composition during a reversible processes with no non-expansionary work,
$$\begin{equation} dU = TdS -pdV \label{eq:internalenergyno_non}\end{equation}$$
Let’s break down those assumptions a bit:
Our battery shouldn’t exchange matter with the environment. Although electrons leave the battery, they also come back in at an equal rate. Otherwise, we can consider the closed circuit as our system.
We assume constant composition for ease. We will loosen this assumption later.
The reason we assumed that all work was expansionary is that we ultimately want to find all the energy associated with heat exchange and expansionary work. We can then remove all of this type of energy and find what energy the battery exchanges with the environment in the form of non-expansionary work. This work will end up to be electrical work!
Because all variables in $\eqref{eq:internalenergyno_non}$ are state variables, there’s a technicality that allows us to ignore the fact that entropy is defined only for reversible processes and simply apply this equation for all processes.
Derived State Variables
Now we need to define two more state variables. First, Helmholtz free energy, $H$,
$$\begin{equation} H := U + pV \end{equation}$$
Second, Gibbs free energy,
$$\begin{equation} G := H - TS \end{equation}$$
Already, you might see that $G = U + pV - TS$, which indicates that $G$ is the internal energy minus expansionary work and heat exchanges. But let’s make that clearer. To do so, we need to look at the changes of these variables. Under the assumption that our system is closed (no matter exchanges) and at constant composition and no non-expansionary work,
$$\begin{equation} dU = TdS -pdV \end{equation}$$
$$\begin{equation} dH = dU +d(pV) = TdS -pdV + pdV + Vdp = TdS + Vdp \end{equation}$$
$$\begin{equation} dG = dH -d(TS) = TdS + Vdp -TdS - SdT= Vdp -SdT\end{equation}$$
The interpretation here is that $dG$ is the change in the energy of the system, removing expansionary work and heat exchanges. It is the energy available to perform work other than work of expansion. It is the work that we can direct from the battery to perform electrical work.