I've always been curious about this. I believe NEC indicates 12AWG for carrying up to 20A, but that has always seemed like a rule of thumb rather than a precise engineering calculation. There are multiple factors involved in actual ampacity including the thermal conductivity/resistance of the insulation and its surroundings.
The calculation of heat generated by passing a current through a wire is easy; for example, 12AWG wire presents 1.6 ohms of resistance per thousand feet (1.6mOhms/ft). So the power dissipated (heat generated) by passing 20A of current through 1ft of 12AWG wire is I^2 R or 20*20*0.0016 = 0.64W.
The question then is how much the temperature of that wire will rise before insulation fails or other thermal damage. To know this, we must understand how much resistance there is to transfer of heat from the wire into the environment which depends on the insulation material and thickness, air, earth, other wires, foam insulation, etc.. It also depends on the presence of other conducting wires (often there will be at least two in the same bundle, each dissipating heat.
This site offers a good discussion:
http://electrician2.com/articles/ampacity.htmI'm not an EE, so if I've botched any of the above, please set me straight. If anyone knows of a good online calculator for ampacity that's more precise, please share it! At the least, it would be great to have one that lets you calculate ampacity for a variety of common materials (e.g. 12/2 Romex) and environments (air, conduit, wall).