This is a long answer. (Last night I suffered a bout of physics disease. This is the nervous compulsion to solve a physics problem because it bugs you.)
At the time of writing, other answers on this site are as follows.
. AndyS says it is because the gas expands adiabatically
. Markus Deserno says it is a Joule-Thomson expansion (constant enthalpy)
. Ron Maimon describes the gas pushing back the surrounding atmosphere; this
is adiabatic expansion again, apparently in the room containing the can
. leftaroundabout says "to do this properly" and briefly describes adiabatic expansion from a molecular point of view
. bigjosh says it is to do with a liquid-to-gas phase change inside the can
. ManRow says adiabatic and presents some formulae
. Prof. Gopal Lohar says Joule-Thomson
. John Darby acknowledges that there is irreversibility at the nozzle and
then offers another treatment of adiabatic expansion
. Anthony X says the cooling is wholly due to evaporation inside the can,
and explicitly not any subsequent expansion
and I have ignored a few other answers. So most people are arguing for adiabatic expansion. However, the process inside the nozzle of any spray can is certainly not
adiabatic (in the sense of not isentropic), and that is where most of the pressure drop occurs. Therefore the answer cannot be that simple.
Nevertheless in the following I will first present the adiabatic expansion of a gas, to see what it offers, and then move on. It will turn out that this calculation will be valuable at the end.
I will present calculations for the case of butane (C$_4$H$_{10}$) gas.
I have often used
butane gas cylinders while camping and they do indeed get quite cold
after a few minutes of use. Here are some properties.
$$
\begin{array}{ll}
\text{adiabatic index of butane gas at $300$ K} & \gamma = 1.09 \\
\text{constant volume specific heat capacity} & c_V = 1.57 \, {\rm kJ/kg K} \\
\text{constant pressure molar heat capacity} & C_{p} = 98.5 \, {\rm J/mol K} \\
\text{ditto for liquid} & C_p = 130\,{\rm J/mol K} \\
\text{molecular weight} & 58.124 \, {\rm g/mol} \\
\text{boiling point (at 1 atm)} & -0.4^\circ\,{\rm C} \\
\text{latent heat of vapourization} & L = 22.44\,{\rm kJ/mol} \\
\end{array}
$$
$$
\begin{array}{cc}
\text{temperature ($^\circ$C)} & \text{vapour pressure (kPa)} \\
-0.5 & 101 \\
10 & 170 \\
19 & 200
\end{array}
$$
Adiabatic expansion
First let's consider adiabatic expansion.
The physical basis of the cooling in adiabatic expansion is energy and
momentum conservation.
The gas pushes against the rest of the atmosphere, doing work on it. Therefore
its internal energy falls (since no heat is coming in). In a system
as simple as a gas, such a drop in internal energy results in a drop in temperature (the molecules slow down). Under the approximations of constant heat capacities and ideal gas, we have $pV^\gamma = $ constant,
and therefore, after using $pV = RT$ per mole, we have
$$
p^{1-\gamma} T^\gamma = \mbox{constant}
$$
so
$$
T_{\rm f} = T_{\rm i} (p_{\rm i} / p_{\rm f})^{(1-\gamma)/\gamma}
$$
where subscript i and f stand for 'initial', 'final'.
Here $\gamma$ (the adiabatic index) is equal to $C_p/C_V$; it has the value
$1.09$ for butane. The vapour pressure of butane at room temperature is about
2 atm, so let's take the pressure ratio to be $2$. Then we find
$$
T_{\rm f} / T_{\rm i} = 0.94
$$
so if the initial temperature is $293\,$K then the final temperature is
$277\,$K which is about $4^\circ$C. So that's a cooling in the right ball-park,
and one may think we are on the right track.
But are we?
Joule-Thomson expansion
The trouble with the previous calculation is that it seems to be
assuming that while we spray the gas, the pressure just outside the nozzle
is 2 atmospheres. But that is an impossible statement. You can't create
a whole atmosphere of pressure difference in air just using a spray can!
It would cause the gas just outside the nozzle to rush explosively outwards, reaching
the speed of sound in a microsecond or so. It is quite impossible.
(An atmosphere of pressure is $10^5$ N/m$^2$; that's a large pressure.
Recall that
weather charts record pressure variations in millibars; pressure changes in
weather are at the percent level).
So how does the pressure go from 2 atm (the vapour pressure in the can)
to 1 atm (the ambient pressure in the surroundings)? The answer is simple: the
pressure drop is almost entirely in the nozzle. The pressure outside the nozzle
is somewhat above one atmosphere, so that gas does spray away from the nozzle,
but compared to one atmosphere this does not present much of a
ratio, so the adiabatic
expansion as the gas moves away from the nozzle is not able to account for
much of the observed cooling at the can.
So let's consider what happens inside the nozzle. Here the process is, to
good approximation, a pressure drop in a constriction without heat exchange.
This process is called the Joule-Thomas process. It is a well-studied process
much used for cooling and liquifying gases.
If the conditions are right then one gets cooling, quantified by the
coefficient
$$
\mu_{H} = \left( \frac{\partial T}{\partial p} \right)_H
= \frac{1}{C_p} \left( T \left( \frac{\partial V}{\partial T} \right)_p
- V \right)
$$
For the ideal gas this gives $\mu_H = 0$ so the cooling is related to
the degree to which the gas is not well described as an ideal gas, for example
near the liquid/gas phase transition. I tried to find values for this
coefficient for butane online. I got some numbers but was unsure, so for
added confidence I used the van der Waals model. This is the model where
the equation of state for one mole is
$$
(p + a/V^2)(V-b) = RT
$$
and you can look up the constants $a$ and $b$ for your chosen gas. It's not a
perfectly accurate model but pretty good. For the van der Waals gas one finds
(for one mole)
$$
\mu_H =
\left( \frac{\partial T}{\partial p} \right)_H
= \frac{V}{C_p} \left( \frac{2 a (V-b)^2 - R T V^2 b}
{R T V^3 - 2 a (V-b)^2} \right)
$$
(and this is roughly $(2a/RT \; - \; b)/C_p$).
For butane one finds
$$
a = 1.466\;{\rm Jm}^3/{\rm mol}^2,
\;\;\;\;\;\;\; b = 1.226 \times 10^{-4} \;{\rm m}^3/{\rm mol}
$$
and at or just below room temperature the above formula gives
$$
\mu_H = 12\,{\rm K/MPa}.
$$
This is of the same order of magnitude as some values I managed to find on
the web. So we find that in a pressure drop of 1 atm = $0.1\,$MPa, the
temperature drop suffered by butane gas in a Joule-Thomson process
near room temperature is $1.2$ K. So we conclude that the observed cooling
(10 K or more) is not primarily owing to the Joule-Thomson process
in the nozzle.
Evaporative cooling
So far we have found that the answer is not adiabatic expansion after
the nozzle, nor is it Joule-Thomson (isenthalpic) expansion in the nozzle.
So we have to look inside the can for our answer.
I will first treat the case where there is liquid (and vapour) in the
can, and then the case where only gas is involved.
With liquid in the can, let's suppose in the first instance that there
is not enough time for significant heat flow into the walls of the can.
In this case we have evaporative cooling. When some vapour escapes through
the nozzle, the pressure in the can falls, and consequently some liquid
evaporates. Both liquid and vapour then cool. For a long time I found this
cooling puzzling from a thermodynamic point of view. From a molecular point
of view it is straightforward (if hard to calculate): the faster molecules
preferentially move out of the liquid, and on their journey they slow
down because they are escaping from attractive forces in the liquid.
But how to calculate this using thermodynamic properties? When
the pressure first falls, enough liquid evaporates to keep the system
on the $p-T$ coexistence curve, which has a positive slope so the lower
pressure corresponds to a lower temperature. Eventually the system reaches
a dynamic equilibrium with pressure approximately 1 atm outside the nozzle,
pressure somewhere between 1 and 2 atm inside the can, and temperature somewhere
between the temperature where the vapour pressure is 1 atm and the temperature
where the vapour pressue is 2 atm. These two temperatures are
$-0.5^\circ$C and $19^\circ$C. Hence if the can is left spraying out gas
then the temperature will fall steadily until it reaches about $-0.5^\circ$C
(in the case of butane).
This tells us how to calculate, but the original question was why does the
cooling happen?
Consider the specific latent heat $L$.
This is the heat you would have to provide to cause unit mass of substance to change from
liquid to gas in conditions of constant pressure and temperature. This is the case we
are used to when we boil water in a kettle. But evaporative cooling is
a different process. No heat is provided, but we do something which lowers
the pressure (e.g. by moving a piston, or by allowing gas to escape).
If mass $m$ of liquid now turns to gas, then the internal energy of that
part of the whole system has gone up by about $L m$ (well, to be precise, it keeps some of this energy and also transfers energy to the surroundings by doing work as it expands). Where has this energy $Lm$
come from? It came from the rest of the system as internal energy
moved from the rest
of the system to this part. So the temperature of the rest of the system
must fall by
$$
\Delta T \simeq \frac{L m}{C_p}
$$
where $C_p$ is the heat capacity of the rest of the system, which
can be written
$C_p \simeq M c_p$ where $M$ is the mass of the whole and $c_p$ is the specific
heat capacity. So we have
$$
\Delta T \simeq \frac{L}{c_p} \frac{m}{M} \simeq 220 \frac{m}{M} {\rm kelvin}
$$
which is valid for $m \ll M$. I am here just giving some rough feel for
the sorts of numbers involved. It says we can expect a temperature drop
of order $20^\circ$C if we spray out one tenth of the contents. As I
already said, the temperature will not fall indefinitely; it reaches
a new equilibrium; the purpose of this rough calculation was merely to
show that the energy movements are consistent.
The return of adiabatic expansion
The summary of the above discussion is that if there is liquid in the can
then the temperature drop is primarily owing to evaporative cooling of that
liquid, and the vapour, because the latent heat of vapourization has to be
provided
by the contents of the can in the absence of heat flow from outside.
But what if there is no liquid in the can? What if it is simply a high
pressure gas?
We already established that there is only a modest
cooling after the gas has left the nozzle and makes its way into the room.
Let's look inside the can again.
To understand the effect of a leak in a can of gas, imagine a thin membrane
dividing the gas which is about to escape from the gas which will remain. As
the gas escapes this membrane moves and the gas within it expands. That
expansion is, to good approximation, adiabatic. To prove this we need to claim
that there is no heat transfer across this membrane. There will be no heat transfer
if the gas on either side of the membrane is at the same temperature.
If you think it is not, then allow me to add another membrane further down,
dividing the gas which will remain into two halves. This gas is all simply
expanding so there is no reason for temperature gradients within it. But
this argument will apply no matter where we put the membrane.
We conclude that the part of the gas which remains in the can simply
expands adiabatically to fill the can. So now the initial calculation
which I did, describing adiabatic expansion, is the right calculation,
but one must understand that the process is happening right in the can! So
no wonder the can gets cold!
Here is another intuition for this. As it moves through the nozzle,
the gas that gets expelled is being worked on by
the gas that gets left behind, giving it energy, and the gas left behind loses
energy. If there were a big hole the gas would
rush out very quickly. In the case of a narrow nozzle it is prevented from
getting up to very high speed. It that case it makes its way out into the
surrounding atmosphere
at a similar pressure to the ambient pressure and it uses up its extra
energy pushing that atmosphere back to make room for itself.
So why does the can get cold?
In terms of overall energy movements, the can gets cold because the escaping gas does work in pushing back the surrounding atmosphere, and the energy required for this comes from internal energy of the can and its contents. In terms of physical process, the can gets cold for two different reasons depending on its contents. If there is liquid (and therefore also vapour) in the can then the cooling is evaporative cooling. If there is just vapour then the cooling is owing to adiabatic expansion inside the can of the vapour that remains. There is also a slight Joule-Thomson cooling in the nozzle.