Heat is energy transferred because of temperature differences. All other transfers of energy are described as work, $W$ (in J). An example is the work done by a gas under pressure pushing against a piston in an engine cylinder, known as expansion work, $${\rm d}W=\vec{F}{\rm d}\vec{l}=p\vec{A}{\rm d}\vec{l}=p{\rm d}V\qquad,$$ where $A$ is the cross sectional area of the piston (and $\vec{A}$ the vector normal to it) and ${\rm d}l$ the increment by which the piston has moved. Thermodynamics often uses this expansion work motif for its derivations, but the laws of thermodynamics apply to any other kind of work the same.
When calculating the work transferred during a process, the implicit assumption is that the process is quasi-static, i.e. the change occurs slowly enough for the system to be near equilibrium at all times so that intensive state variables change in a uniform manner throughout the entire system. If this is not the case, we describe the process as spontaneous. In this case, different parts of the system change at different rates and along different paths in a phase diagram. Since (expansion) work is defined as the integral of the area under the path in a $pV$ diagram, $$W=\int_{V_i}^{V_f}p{\rm d}V\qquad,$$ this makes it difficult to determine the work done by or on the system. Unless explicitly stated otherwise, the assumption in thermodynamics is that processes are quasi-static. The sign convention for work exchanged is that $W\gt 0$ is done by the system while $W\lt 0$ is done on the system.
Considering a few special cases, we can see that for isochoric processes ($\Delta V=0$), the work done must be zero since the path in the $pV$ diagram is a straight vertical: $$W_V=0\qquad.$$ On the other hand, for an isobaric process ($\Delta p=0$), the pressure can be taken out of the integral and the work done is proportional to the change in volume: $$W_p=p_0\int{\rm d}V=p_0\Delta V\qquad.$$
These equations are valid independent of what the medium is. In order to calculate the work done during other processes, we need to know how pressure and volume depend on one another, i.e. we need to find the equation of state for the medium. For example, to calculate the work done during an isothermal process, for an ideal gas, we can substitute the ideal gas law: $$\begin{eqnarray} W_T&=&\int_{V_i}^{V_f}\frac{nRT}{V}{\rm d}V\\\\\\ &=&nRT\int_{V_i}^{V_f}\frac{1}{V}{\rm d}V\\\\\\ &=&nRT\ln{\frac{V_f}{V_i}} \end{eqnarray}$$
A note about notation: A subscript is often used in thermodynamics to denote a state variable that is kept constant during a change or as a measurement is taken, so e.g. $W_V$ indicates work done at constant volume.
We have seen earlier that state variables do not depend on the history of the system; they are path independent.
However, work clearly does depend on the path since the work done along a direct path in the $pV$ diagram will
in general be different from the work done via a path involving an intermediate state.
Work is not a state variable but a process variable.
The same is also true for heat.
The
First Law of Thermodynamics
relates the heat and work transferred during a process to the change of the
internal energy
of the system subject to these transfers:
$$\Delta U=Q-W$$
The net energy transferred equals the change in internal energy of the system.
While this is intuitive - the net amount of energy exchanged with the system is the same as the change of the energy
contained in the system - it is quite remarkable because both heat and work are path-dependent process variables,
yet their combined effect changes a state variable, which doesn't depend on the path taken.
The First Law is based on a sign convention for the direction of heat and work flows. Heat entering and work leaving the system are counted as positive; hence the negative sign in the First Law.
Two interesting cases arise where either heat or work transferred are zero, in which case a single process variable determines the change of a state variable: For isochoric processes we have $$\Delta V=0\Rightarrow W_V=0\Rightarrow\Delta U=Q_V\qquad,$$ while for adiabatic processes, i.e. processes during which no heat is transferred, $$Q=0\Rightarrow\Delta U=-W_Q$$ work is the only determining factor.
Given that state variables are constant and uniform throughout a system in equilibrium, we can combine them in arbitrary ways and create other state variables for convenience (as long as the units match up). The most important "synthetic" state variable is enthalpy, $H$ (in J), a measure of internal energy plus the potential to do expansion work, expressed as the product of pressure and volume (whose units combine to those of an energy as we have seen above): $$H=U+pV$$ By taking the total differential $${\rm d}H={\rm d}U+p{\rm d}V+V{\rm d}p$$ and using the First Law to replace the internal energy by the difference between heat and work transferred, we have $$\qquad=Q-W+p{\rm d}V+V{\rm d}p\qquad.$$ For expansion work, the second and third terms cancel: $$\qquad=Q+V{\rm d}p\qquad.$$ This makes enthalpy very useful for isobaric (${\rm d}p=0$) processes since this allows us to determine a state variable by measuring a process variable: $${\rm d}H=Q_p$$ - the change in enthalpy of the system is the amount of heat transferred at constant pressure.
Therefore, it is common to calculate energy balances in terms of internal energy for isochoric systems (such as e.g. boilers) but in terms of enthalpy for isobaric systems (such as e.g. biological cells).
An example of a process where enthalpy, but not internal energy, is conserved is adiabatic expansion, also known as adiabatic throttling. This process is part of the thermodynamic cycle operating in a refrigerator. A throttle is a section of pipe with a reduced diameter. If a liquid close to its boiling temperature is pumped through a throttle, it will evaporate as it expands into the larger volume available at the end of the reduced section. If the throttle has adiabatic walls (i.e. is thermally well insulated), no heat can be transferred in this process $$Q=0$$ and the expansion work done amounts to $$W=p_2V_2-p_1V_1\qquad,$$ fueled by the latent heat of vaporisation of the medium. The change in internal energy across the throttle is therefore $$\Delta U=Q-W=-W\qquad.$$ However, enthalpy, which takes into account expansion work explicitly, remains constant: $$\Delta H=\Delta U+p_2V_2-p_1V_1=0\qquad.$$
We can use the throttling process in a refrigerator by including a throttle in a cycle. Since thetemperature will change during the throttling process (as no heat can be transferred), we can use the cold gas in a heat exchanger to cool down the inside of the fridge. Of course, work has to be supplied to keep the process going: A compressor transforms the gas back into a liquid and pumps it around the circuit. In the $pV$ diagram, the medium used in the refrigerator cycle alternates between a high-pressure, low-volume state and a low-pressure, high-volume state. This cyclic process runs in an anti-clockwise direction in the $pV$ diagram because work is done on the system rather than by it.
Next, we will investigate cyclic processes further and establish the thermodynamic limits of work that can be extracted from a system for given inputs.