What's been said so far is pretty much accurate, although I would assign more of a role to pressure than what has been asserted so far. The Browning inertial recoil design used in most modern semi-autos uses the pressure to lock the barrel and slide together until the bullet has left the barrel, so the barrel is literally trying to tear itself out of the slide under pressure, and to that should be added the force of the bullet slamming into the rifling, which depends largely on the weight of the bullet. All Browning-pattern semi-autos have fairly tiny slivers of metal holding the barrel and slide together, and if pressures above the tensile strength of the metal are experienced or if there is excessive endplay, these tiny bits of metal will give slightly. As the metal gives way, the barrel gets more of a run-up at the locking lug(s) under pressure, increasing the amount of impact force it brings to the relationship, and so on. As headspace opens up, more of the case is left unsupported.
9mm vs. .40 S&W is an interesting comparison. Let's take some 9mm at 30K PSI versus .40 S&W at 35K PSI. 9mm case head diameter is 0.394" and .40 S&W case head diameter is 0.424". In each case, area is given by pi * (d / 2)^2, so 9mm has a case head area of 0.122 square inches and .40 S&W has 0.141 square inches. Multiplying by pressure, we get 3,660 pounds of pressure for 9mm and 4,935 pounds for .40 S&W.
The amazing thing about all this is that 9mm actually has thicker brass in its web than .40 S&W! Personally, I think .40 S&W would be an incredible cartridge if it were a lightly loaded 10mm, but don't care much for it in its shrunken and, in my opinion, ill-considered current form.