Finding work done for a chemical reaction at non-constant pressure and temperature
There are a series of five assumptions you need to make to get from the general expression for work, to the expression you wrote in your OP.  With each assumption, you are applying successively more constraints to your system.  In the post below I give the assumptions, and the equation that results from each, so that you can see how the form of the equation evolves as successively more constraints are applied.
We start with the general expression for work:
$$\text{đ}w = \vec{F} \cdot d \vec{x}$$
[We use the form $\text{đ}w$ rather than $dw$ because work is path-dependent, and its differential is thus inexact; the dyet glyph, $\text{đ}$, is used to indicate this.]
Now we integrate and express the dot product in terms of scalar quantities:
$$w = \int_{path} \vec{F} \cdot d \vec{x} = \int_{path} |F| \,d |x| \cos(\theta)$$
Because work is path-dependent, it is necessary to integrate over the actual path, as indicated in the above integrals, rather than merely from the initial state to the final state.
Dropping the explict scalar notation, we can write it as:
$$w =  \int_{path} F d x \cos(\theta)$$
Now let's add some assumptions:
Assumption 1: The force is applied in the direction of motion.
As the angle between the direction of motion and the direction of force is $0$, and $\cos(0) = 1$, we have:
$$w =  \int_{path}F dx \cos(\theta) = \int_{path}F dx$$
Assumption 2:  The work is $\bf{pV\text{– work.}}$
For $pV\text{– work}$, we can take work done on the system to be either positive or negative.  I'll use the former convention. And we can only reliably use the external pressure to determine the work; the internal pressure is not always defined, particularly for rapid expansions or compressions.  To indicate this explicitly, we write &quot;$p$&quot; as $p_{ext}$. Then (using &quot;$A$&quot; for area):
$$p_{ext} = F/A;\,\,dV = -A dx\,\, \text{(negative sign is because as $x$ increases, $V$ decreases)}$$
$$\Rightarrow  \int_{path} F dx =  -\int_{path}(p_{ext}A) (dV/A) = -\int_{path}p_{ext} dV$$
Thus:
$$w = -\int_{path}p_{ext}dV$$
This is the general expression for $pV\text{– work}$.  It applies regardless of the conditions.
Assumption 3: The system is in mechanical equilibrium with its surroundings.
In this case, $p_{ext} = p_{sys}$.  By convention, we designate $p_{sys}$ simply as &quot;$p$&quot;.
Note that this assumption restricts us to paths in which the system is continously in mechanical equilibrium with its surroundings.  This limits us to paths whose work depends only on the difference between the initial and the final state.  Thus we do not need to concern ourselves with doing an explicit path integral. I.e., because the path restriction is now implicit, we can simply integrate between intitial and final states (which I've designated as $i$ and $f$):
$$w =  -\int_{i}^{f}pdV$$
Assumption 3a: The system is at constant pressure.
Since $p$ is a constant, we can take it out of the integral:
$$w =  -\int_{i}^{f}pdV=  -p\int_{i}^{f}dV = -p\Delta V$$
I labeled this assumption as &quot;3a&quot; rather than 4 because we didn't need to both specify the system is at constant pressure and in mechanical equilibrium with its surroundings, since if it is at constant pressure it is also in mechanical equilibrium with its surroundings.
But I broke the steps up, since I wanted to give the expression following assumption 3 for completeness.
Assumption 4: The system is an ideal gas.
Substituting $V=nRT/p$, and remembering that the pressure is constant, gives:
$$w =  -p\int_{i}^{f}dV =  -p\int_{i}^{f}d(n R T/p)=-R\int_{i}^{f}d(n T)=-R\int_{i}^{f}ndT-R\int_{i}^{f}Tdn$$
Assumption 5: The temperature is constant.
Since $T$ is a constant, we can take it out of the integral:
$$w =-R\int_{i}^{f}d(n T)=-RT\int_{i}^{f}dn = -RT \Delta n$$
Or, equivalently:
$$w =-R\int_{i}^{f}ndT-R\int_{i}^{f}Tdn = -R\int_{i}^{f}Tdn = -RT\int_{i}^{f}dn = -RT \Delta n$$