There are two very relevant facts that inform this answer: (1) The rest mass energy of a neutron is 1.29 MeV higher than that of a proton. $(m_n - m_p)c^2 = 1.29$ MeV. (2) The total number of neutrons plus protons (essentially the only baryons present) is a constant.
Neutrons and protons can transform into one another via reactions moderated by the weak nuclear force. e.g.
$$ n + e^{+}\rightarrow p + \bar{\nu_e}$$
$$ p + e \rightarrow n + \nu_e$$
Because of the rest mass energy difference, the first of these reactions requires no energy input and the products have kinetic energy even if the neutron were at rest. The second does require energy (at least 1.29 MeV) to proceed, in the form of reactant kinetic energy.
In the first second of the universe, with temperatures higher than $kT >10$ MeV ($10^{11}$K) these reactions are rapid, and in balance (occur with almost equal likelihood) and the $n/p$ ratio is 1. i.e. Equal numbers of neutrons and protons.
As the universe expands and cools to less than a few MeV (a few $10^{10}$ K) two things happen. The density of reactants and the reaction rates fall; and the first reaction starts to dominate over the second, since there are fewer reactants with enough kinetic energy (recall that the kinetic energies of the particles are proportional to the temperature) to supply the rest mass energy difference between a neutron and proton. As a result, more protons are produced than neutrons and the $n/p$ ratio begins to fall.
The $n/p$ ratio varies smoothly as the universe expands. If there is thermal equilibrium between all the particles in the gas then the $n/p$ ratio is given approximately by
$$\frac{n}{p} \simeq \exp\left[-\frac{(m_n-m_p)c^2}{kT}\right],$$
where the exponential term is the Boltzmann factor and $(m_n - m_p)c^2 = 1.29$ Mev is the aforementioned rest-mass energy difference between a neutron and a proton. The rate at which $n/p$ changes is simply determined by how the temperature varies with time, which in a radiation-dominated universe is derived from the Friedmann equations as $T \propto t^{-1/2}$ (since the temperature is inversely related to the scale factor through Wien's law).
In practice, the $n/p$ ratio does not quite vary like that because you cannot assume a thermal equilibrium once the reaction rates fall sufficiently that the time between reactions is comparable with the age of the universe. This in turn depends on the density of all the reactants and in particular the density of neutrinos, electrons and positrons, which fall as $T^3$ (and hence as $t^{-3/2}$). At a temperature of $kT \sim 1$ MeV, the average time for a neutron to turn into a proton is about 1.7s, which is roughly the age of the universe at that point, but this timescale grows much faster than $t$.
When the temperature reaches $kT = 0.7$ MeV ($8\times 10^9$K) after about 3 seconds, the reaction rates become so slow (compared with the age of the universe) that the $n/p$ ratio is essentially fixed (though see below$^{*}$) at that point. The final ratio is determined by the Boltzmann factor $\sim \exp(-1.29/0.7)= 1/6.3$. i.e. There are six times as many protons as neutrons about three seconds after the big bang.
$^{*}$ Over the next few minutes (i.e. after the epoch talked about in our question) there is a further small adjustment as free neutrons decay into protons,
$$ n \rightarrow p + e + \bar{\nu_e}$$
in the window available to them before they are mopped up to form deuterium and then helium. During this window, the temporal behaviour is
$$ \frac{n}{p} \simeq \frac{1}{6} \exp(-t/t_n),$$
where $t_n$ is the decay time for neutrons of 880s. Since the formation of deuterium occurs after about $t \sim 200$s this final readjustment gives a final n/p ratio of about 1/7.