Although the exact whys and wherefores are lost to antiquity, it is generally believed by historians and archaeologists that early man first used dogs (or quite possibly, wolves) for two functions: to help hunt and to protect their mutual home. Both dogs and humans enjoyed multiple benefits from their mutual relationship, and while using dogs in hunting probably isn't as popular as it was in prehistoric times, many people do find their dogs' protective instincts helpful in their homes. Certainly some dog breeds are more disposed to behave protectively than others, but many of us have certainly known Pekingese, Chihuahuas, and even Dachshunds whose barks (and hearts) were bigger than you might expect when it came to protecting their homes and humans.
The Root of the Behavior
It is worth observing that dogs display two closely related behaviors, protectiveness and possessiveness, and that the two are not easy to distinguish from each other at first. The two behaviors are outwardly similar, and both involve aggressive and/or warning behavior towards people or other animals encroaching on what the dog perceives as 'his' territory and possessions or his humans' territory and possessions. So a dog that uses its body to block another dog from getting at his food while he is eating is behaving in a possessive manner. A dog that jealously guards a toy so that other dogs cannot get it is also acting possessively.
The distinctions between canine protectiveness and possessiveness become more finely drawn when dealing with humans, however. If a stranger approaches your house, either in daylight or darkness, one would expect a dog to bark once he noticed the stranger approaching. However, if the stranger turned out to be a friend, coworker or other acquaintance who had simply never been to your home -- and you invited them in -- a well-socialized dog would reduce his aggression toward the visitor. However, a poorly-socialized dog would continue to behave possessively, displaying aggression toward the visitor even when his owner made it clear that the dog's behavior was inappropriate.
A well-socialized dog will learn, over time, which behaviors are correct and which are incorrect; it all comes down to proper training and socialization from their humans. Some breeds are more disposed to engage in protective behavior; the American Kennel Club notes that German Shepherds, Doberman Pinschers, Rottweilers, and Bull Mastiffs are well-suited to protective roles, as well as breeds like Pulis, Giant Schnauzers, and Rhodesian Ridgebacks. That said, even smaller and toy breeds can benefit from training that socializes them properly and redirects or eliminates aggressive impulses.
Encouraging the Behavior
There are a number of key points that should be focused on when socializing or training a dog to reduce aggressive and possessive behavior, and to redirect it towards more protective behavior, like defending the home. Assuming you don't rely on a professional trainer and choose instead to do the training yourself, you'll first want to determine what stimuli trigger aggression on your dog's part. Then, having determined what sets him off, you can focus on desensitizing him to those triggers. If a stranger approaching the yard makes him demonstrably aggressive, then use positive and negative reinforcement to train him to behave more calmly. A few barks and a growl are one thing, but frantic, excited, nonstop barking is quite another.
Getting your dog to look to you for leadership is another key point. If your dog is confronted with with an unfamiliar situation, you don't want him falling back onto aggressive behavior. Train your dog to accept you as 'pack leader' and use reinforcement to educate him on the behaviors you want him to display, including impulse control. Commands like sit, stop, stay and stand are excellent ways to build and reinforce impulse control in a dog.
Once your dog has learned some impulse control, you can build confidence by confronting him with strangers. Have a friend or neighbor approach your home or yard, and allow the dog to see or hear the approaching stranger. Ideally, the dog will alert with barks, but not chase or attack the stranger. If the dog overreacts, give immediate verbal corrections. If the dog reacts appropriately, reward him. You can also consider reinforcing the behavior by having the 'stranger' run away in mock fear.
Other Solutions and Considerations
Calmness and assertiveness are indispensable when teaching your dog guarding behavior. Obviously, positive reinforcement (treats or a toy as a reward for good behavior), and negative punishment (withholding a treat or toy), coupled with verbal feedback are most effective with dogs; these are classic operant conditioning methods. It should go without saying that positive punishment (administering actual punishment) should never be used as part of dog training for any reason. If you prefer professional help, individual and group training is available from many sources. Ask your vet for referrals; he may well be familiar with some of the dog trainers in your area.
Dogs instinctively act to defend themselves and, by extension, their family unit and 'den'. Since your home is your dog's 'den,' you can take advantage of those instincts as well as manage and redirect them, in ways that increase your home's security. While there are plenty of professionals willing and able to train your dog toward protective behavior, it is certainly something you can do yourself with patience, persistence and determination.