A closed system is one where no material is transferred across the system boundaries. Only heat and work are transferred to the system as shown in the animation above.
As work is done on a real gas, the temperature and pressure increase and some heat will be transferred out of the system.
As a real gas expands, it does work on the surroundings and the temperature and pressure decrease. Heat is transferred to the system.
We balance the changes in the internal energy of the gas with the amount of heat transferred to/from the gas and work done by/on the gas. This energy balance is called the first law for a closed system. It is written
differential form dU = dQ - dW
integrated form (1)
The sign conventions for heat transfer and work are
Heat transferred to the system is POSITIVE
Work done on the system is NEGATIVE
If the processes by which heat is transferred or work done on/by the system are reversible, then we can calculate numerical values of for the Q and W in equation (1) and work out the internal energy change.
If the processes by which heat is transferred or work done on/by the system are irreversible then, values at the start and end of the irreversible process are required to calculate the change in internal energy.