Consider a system consisting of some quantity of a gas enclosed in a cylinder fitted with a movable, massless and frictionless piston.

A system enclosed in a cylinder
During expansion of the gas, molecules colliding with the piston impart momentum to the piston. The time rate of change of momentum is the force, F exerted by the gas on the piston. If dx is the displacement of the piston, the work done by the gas, dW = F dx. If A is the area of cross section of the piston, the pressure exerted by the gas, P = \(\frac FA\).
Hence, the work done, dW = PAdx = PdV where dV = Adx is the increase in the volume of the gas. Here, dx is the infinitesimal displacement of the piston and dV is the infinitesimal increase in the volume of the gas.
If Vi is the initial volume of the gas, and Vf is the final volume, the total work done by the gas in moving the piston is given by W = \(\int^{V_f}_{V_i}\)pdV.