Dogs wag their tails to convey that they are happy, friendly and excited... Very true in a lot of cases, but dogs also use their tail to communicate, along with facial expressions and body postures. We should see a wagging tail as a sign that the dog is willing to interact, not a sign of a particular mood. In other words a wagging tail is a good indication that the dog is engaged with what is going on in their environment, not how they may react to what is going on.
Dogs have a kind of language that's based on the position and motion of their tails. When a dog is relaxed, its tail will be in its "natural" position. This position will differ from breed to breed. If a dog is nervous or submissive, it'll hold its tail lower than its natural position, and will tuck its tail under its body if it's very scared. On the opposite end of the spectrum a stiff, vertical tail may indicate aggression and a tail held straight out means the dog is curious about something. Generally a loose, flowing wag is good, whereas a stiff wag can show tension or hostility. A fast wag is also generally a good sign, but a slow wag may mean the dog will not be friendly. Then there’s the “full body wag,” where the dogs bottom or whole body is relaxed and moves back and forth. A big tail wag where the bottom is moving and the tail is making wide sweeping motions generally indicates a friendly dog who’s ready for a friendly interaction with a person or other dog!
A dog also uses its tail in other ways such as acting as a rudder when swimming and acting as balance when running. Just watch a dog take a turn at high speed and you will see them use their tail for stability.