Then what does apply in an irreversible process? Well, gases (even ideal gases) are viscous fluids in which the state of stress is described by the equations for a Newtonian fluid. The Newtonian fluid equation predicts that the normal compressive stress (normal force per unit area) at the piston face is given, not by the ideal gas law, but by: $$\sigma=\frac{RT}{v}-2\mu\frac{\partial V}{\partial z}$$$$\sigma=\frac{RT}{v}-2\mu\frac{\partial V}{\partial z}\tag{1}$$where v is the specific molar volume of the gas evaluated at the inside piston face, T is the temperature of the gas at the piston face, $\mu$ is the gas viscosity, V is the velocity of the gas (which varies with spatial position within the gas), and z is axial position along the cylinder. The first term on the right hand side of this equation is the pressure predicted by the ideal gas law (with all the parameters evaluated locally at the inside piston face), and the second term is the contribution of viscous stresses to the overall compressive stress at the piston face. Note that, for a reversible (quasi static, very slow deformation), the second term is negligible. Furthermore, for a reversible process, the specific volume, temperature, and pressure are uniform within the cylinder.
The net result of all this is that, for an irreversible process, the force per unit area that the gas exerts on the inside piston face is not determined solely by the volume of the gas but also by the rate of change of volume with time. This is the reason that the ideal gas law does not describe the force per unit area of the gas on the piston in an irreversible process. However, as indicated above, in the end, if the final state of the gas is again one of thermodynamic equilibrium, the work done by the gas on the piston will be equal in magnitude and opposite in sign to the work done by the surroundings on the piston (assuming the piston is oriented horizontally, so that its potential energy does not change).
ADDENDUM
For a horizontal cylinder, the instantaneous force balance on the piston is $$(P_g-P_o)A=m\frac{dv}{dt}$$where $P_g$ is the force per unit area that the gas exerts on the inside face of the piston (a function of time, referred to as $\sigma$ in Eqn. 1 above), $P_0$ is the force per unit area exerted externally on the outside face of the piston, A is the cross sectional area of the piston, m is the mass of the piston, and v is the velocity of the piston. If we multiply both sides of this equation by the velocity $v=\frac{L}{dt}$ and integrate with respect to time from t = 0 to t = t, we obtain: $$\int_{V(0)}^{V(t)}{P_gdV}=\int_{V(0)}^{V(t)}{P_odV}+m\frac{v^2(t)}{2}\tag{2}$$where V(t)=AL(t) is the gas volume at time t. The left hand side of this equation is $W_g(t)$, and represents the work done by the gas on the piston up to time t, and the integral on the right hand side represents $W_o(t)$, the work done by the outside face of the piston on the surroundings up to time t: $$W_g(t)=W_o(t)+m\frac{v^2(t)}{2}\tag{3}$$The piston may oscillate about its final equilibrium position as time progresses, but, eventually, as a result of the viscous damping stresses in the gas, the piston will come to rest (even if the piston is frictionless), and its kinetic energy will have dissipated. At this point, the gas will again be at equilibrium, and we will have $$W_g(\infty)=W_o(\infty)$$Thus, at final equilibrium, the work done by the gas on the inside face of the piston will exactly match the work done by the outside face of the piston on the surroundings.
For the case of a vertical piston, the analogous result is: $$W_g(\infty)=W_o(\infty)+\frac{mg}{A}(V(\infty)-V(0))$$In this case, if the piston is regarded as part of the surroundings, then the left hand side of this equation again represents the work done by the gas on its surroundings.
For the answer to your second question, if L(t) represents the distance of the piston from the dead end of the cylinder, the velocity of the piston is $$v(L)=\frac{dL}{dt}$$If the deformation of the gas is assumed to be homogeneous (a uniform function of x), we have: $$\frac{dv}{dx}=\frac{1}{L}\frac{dL}{dt}$$But, since the volume of gas is V=AL, we have that:$$\frac{dv}{dx}=\frac{1}{V}\frac{dV}{dt}\tag{4}$$ This establishes the connection between the viscous term in the expression for the compressive stress and the rate of change of volume.
I realize that, in this development, I've massed around a little with the symbols v and V, in some cases representing specific volume, velocity, and total volume, but you can figure out which one I'm referring to from the context of the discussion. Sorry about that. There were 3 parameters for which I wanted to use v, and only 2 symbols.