Free energy is energy that is available to do work. Maximizing the free energy gain and the gain in work that can be extracted from a system is important for a wide variety of physical and technological processes, from energy harvesting processes such as photosynthesis to energy storage systems such as fuels and batteries. This paper extends recent results from non-equilibrium thermodynamics and quantum resource theory to derive closed-form solutions for the maximum possible gain in free energy and extractable work that can be obtained by varying the initial states of classical and quantum stochastic processes. Simple formulae allow the comparison the free energy increase for the optimal procedure with that for a sub-optimal procedure. The problem of finding the optimal free-energy harvesting procedure is shown to be convex and solvable via gradient descent.