Many people purchase their first gun for home defense, and the question of which type of gun to pick is a common one. The three basic choices are a handgun, shotgun, or rifle. Of the three, I would recommend a shotgun for most situations, with handguns being recommended for people who lack the physical ability to handle a shotgun. My recommendation for the “ideal” home defense shotgun, and the pros and cons of each of these firearm types, in the order which I recommend them, are discussed below.

My ideal home defense firearm is a 12 gauge pump action shotgun with an 18″ barrel and a magazine capacity of 6 shells or more, such as the Remington 870 XCS Marine Magnum. 12 gauge is the most common shotgun gauge these days, and 12 gauge shells are amazingly effective when loaded with 00 buckshot (pronounced “double ought buckshot”). The 18″ barrel is the minimum length generally allowed by law, and does not make the gun too cumbersome inside a home. The pump action is the pinnacle of reliability, and makes it easy to keep the gun stored safely but ready to use at a moment’s notice. The gun will be amazingly effective at close range, and the risk of over penetration through exterior walls and subsequent injury to neighbors is lower than with a rifle or handgun.
The gun I listed as my 1st choice, a Remington 870 XCS Marine Magnum, costs about $900, which is nearly three times as much as as basic pump action shotgun such as the Remington 870 Express. The benefit of paying for this more expensive gun comes in the form of better build quality, a corrosion resistant coating, a nice recoil reducing pad on the stock, and the ability of the gun to easily accept magazine extensions. If you feel like spending more on the Remington 870 XCS Marine Magnum go for it, and if not the standard Remington 870 will still provide excellent protection for your home and loved ones.

Remington 870 XCS Marine Magnum

1st Choice: A Shotgun:
Pros: Incredibly effective. Less risk of injury to neighbors due to over penetration of walls.
Cons: Harder to maneuver with in tight spaces due to larger size than handguns.
I would generally recommend a shotgun for home defense, because shotguns are (rightfully so) generally considered to be the most effective short ranged firearm. Rather than shooting a single bullet with each pull of the trigger as a rifle or handgun would do, shotguns shoot multiple pellets. These multiple pellets are more likely to stop an attacker but less likely to pass through the exterior walls of your home and injure your neighbors (note that I say *less* likely, as there is always a risk to bystanders). The recoil from firing a shotgun is heavier than a handgun, but this heavier recoil is more than worth the increase in effectiveness (although there is more to the effectiveness of a firearm than the kinetic energy of its projectiles, you can compare the 500 ft-lbs of energy from a .45 caliber bullet to the 2000+ ft-lbs from a shotgun firing 00 buckshot). For those who are new to guns and not accustomed to recoil, let me assure you that you will quickly get used to it after a few trips to your local range. For those who have a physical condition that does not allow them to tolerate heavier recoil, I would suggest acquiring a gas operated semi-automatic shogun (such as the Remington 11-87) which will be more gentle than a pump action or double barreled shotgun. Another option is to use low recoil shotgun shells, which as the name suggests, will be more gentle than standard shells. Such low recoil shells are going to be slightly less effective than standard shells, but this difference is trivial in the home defense scenario, and some experts recommend low recoil shells for home defense over standard shells.
Another consideration that applies to shotguns and rifles is the length of the firearm. In smaller homes with narrower hallways, a barrel that is too long would make the firearm difficult to maneuver with. For this reason I would suggest a barrel length of 18″, rather than the 30″ barrel which might be more appropriate for hunting or trapshooting purposes.
There are also some accessories you may want to consider for your home defense shotgun.

2nd Choice: a Handgun:
Pros: Easier to maneuver with in tight spaces do to its small size. Lower recoil than most shotguns and rifles.
Cons: Less effective than a shotgun or rifle.
A handgun, such as one from the Springfield XD line of pistols, would be my 2nd choice recommendation for home defense.
Handguns are generally less effective than shotguns or rifles, as their projectiles have less energy, but they do have the benefit of being easier to maneuver with in cramped quarters, such as smaller houses and apartments. Handgun bullets will generally penetrate exterior walls more than shotguns, but less than rifles, meaning that the hypothetical danger to your neighbors is lower than with a rifle but higher than with a shotgun. My recommendations for the proper pistol self defense calibers are here.  Those who are unsure whether they should select a pistol or revolver will find this article helpful.
Another benefit of handguns is that they often have less recoil and can be less intimidating to some people. Indeed my girlfriend is more comfortable with handguns than with rifles or shotguns, as discussed here. You may also wish to consider certain accessories for your home defense handgun.

Remington 7600 Rifle

3rd Choice: a Rifle:
Pros: Generally much more effective than a handgun.
Cons: Rifle bullets have a tendency to over penetrate walls, increasing the danger to your neighbors.
A rifle would be my last choice for a home defense firearm, although it would certainly be preferable over throwing sticks and stones at a home invader.
Rifles are generally more effective than handguns, however rifle bullets are able to penetrate much further than the shot from a shotgun or the bullets from a handgun. This creates a much greater risk to your neighbors, without much benefit to you. Rifles are generally designed for longer range shooting (e.g. 100 yards, and much further +) so there is no appreciable benefit in a rifle designed for long range shooting within your house, since if you are ever forced to shoot a home invader it will be at very close range. Indeed, shooting at someone who is 100 yards away is rather difficult to justify as self defense. Although rifles are great for many purposes, home defense is not usually one of them, since rifles possess the drawbacks of shotguns (heavier recoil, more physically bulky and difficult to maneuver with in close quarters) while having almost none of the advantages (such as less of a risk of over penetration).  That said, pistol caliber carbines can be a viable self defense option.

Now that you have selected a gun for home defense, pick the proper ammunition.

Notes:

When discussing the risk of overpenetration, I assumed that the shotgun ammunition being used was 00 buckshot from a 2.75″ shotgun shell, that the handgun ammunition being used was somewhere between 9mm and .45 caliber, and that the rifle ammunition being used was somewhere between .243 and .30-06. Although the danger of over penetration may be lower with shotguns than with handguns or rifles, the projectile(s) from any gun can over penetrate and pose a danger to innocent bystanders. Even if you are forced to fire your gun in self defense or defense of your family, you should not endanger the lives of your neighbors or other innocent bystanders.

Also, it should go without saying that firing at a human being is the absolute last resort and is only justified to prevent death or grievous bodily harm from being inflicted on yourself or a loved one. The decision to possess a gun carries with it the moral responsibly to use the utmost care and good judgment. Similarly, it is the responsibility of the gun owner to ensure the gun is not stolen by criminals or misused by children, and to comply with any applicable laws on the storage of the firearm.