It relieves tension. It is the way dogs communicate. Most dog owners want their dog to bark if they hear someone at the window or entering the yard.
Dogs are natural warning devices.
However, barking should stop when the dog is commanded to do so. Also, owner absent barking can be quite annoying to others. For this reason, we need to control the behavior when possible. The best way to stop a behavior from reoccurring is to associate it with a negative reinforcement. Effective negative reinforcements vary from dog to dog depending upon several factors including breed, age, device, timing and alike.