Back after the fiasco in Miami , the FBI did a series of tests regarding handgun ballistics and found that the 10mm round had the best penetration and terminal balistics of all the configurations they tried.
The problem was that the 10mm packed a hell of a punch in recoil compared to other rounds. So, to combat that, they started loading the 10mm a little lighter.
Well, it didn't take the industry long to realize that if LE was going to load light, they didn't need such a large case. And thus the .40 S&W was born by taking the 10mm bullet and sticking it into a shorter case.
As for the differences...
Energy on target, and thus the amount of damage a round can do is made up of the weight of the projectile and the speed at which it impacts. The 9mm crowd talk about the fast and light nature of the round and the .45 folk talk about the slow and heavy. The .40 is kind of in the middle. It has both speed and mass although not nearly as high as it's two neighbors. If you look at the 'power factor' of the shooting games, you will see that the .40 isn't really as much energy as a .45, but is significantly more than a 9mm. If you look at a chronograph, you'll see that the .40 tavels closer to the 9mm in speed than the .45.
In ballistic tests, the .40 usually penetrates to the 90+% of a .45 and in the hollow point mushrooms with consistacy of a 9mm. In fact, some of the latest .40 rounds will mushroom to about the same diameter that a .45 will mushroom.
In practice, it's really the skill of the shooter that will matter most. Good solid hits will stop the attack with any of the rounds.
The .40 is a higher pressure round. As a result, there is no +P and reloaders should use care in measuring their powder to not exceed the SAMMI pressure spec of the round.
In terms of ammo, the average .40 will hold one or two rounds more than a similar size .45, but a 9mm may hold as many as 3 or 4 rounds more.
An interesting hybrid is the 357 SIG. This round uses the 9mm (.355") bullet in a necked down case that is similar to the .40. (The .357 SIG is NOT a necked .40, the case walls on the .357 SIG are significantly thicker.) This round brings the 9mm projectile into a new world of ballistics with significantly more velocity. The design criteria of the round was to equal the muzzle ballistics of a .357 magnum round in an auto. From all accounts, it is a success. Well, except for the fact that ammo is a bit on the expensive side.
Some people shoot the .357 SIG purely because it has a very unique sound due to the high muzzle velocity.
Most semi-autos can convert from .40 S&W to the .357 SIG with nothing more than a barrel change.
My current carry gun, the SIG 239 comes as either a 9mm, .40, or .357 SIG. I currently use the .40 configuration as I find ammo cheaper and easier to pbtain than the .357 SIG, and I prefer the higher energy of the .40 round over the 9mm.. For me, the grip size of a .40 fits my hand better than a .45 and the reduced size of the gun helps to conceal it better.
Probably a lot more than you wanted to know, but there it is. (^_^)