Boyle's Law. When you compress a gas, it gets warmer. When the gas expands, it gets cooler. When the gas is released from an aerosol can, it expands and cools, thus cooling the can. The same principle is used in air conditioners.   