Dogs wag their tails for many reasons. At the most basic level, a wagging tail simply means the dog is responding to something in their environment. They are trying to convey how they are feeling about what is happening, and that can help people understand what kind of interaction the dog is attempting to have1. The position of the tail and the speed of the wag can also indicate different emotions.
For example, when a dog is expressing happiness or friendliness, their tail is often in a neutral position (the base of the tail is parallel to the ground). It can also be in a slightly upward or downward position and wagging at a moderate speed. Their tail is relaxed and wagging in full, sweeping side-to-side movements1.