Dogs wag their tails as a form of communication. As well as happiness, tail-wagging also expresses anger and agitation. A person should look at the entire body of the dog to determine the emotion - stiffened muscles, tension and ears pinned back are signs that it is best to leave a dog alone.
Dog originally used their tails for balance. The tail keeps the dog from falling over when it has to make a turn while running. It also helps the dog balance while climbing or jumping.
Dogs do not wag from birth. Most dogs begin wagging at the age of a month and a half, when it becomes necessary to communicate with their mothers or other members of the litter.