People ask why dogs wag their tail. Laypeople (non-experts) generally believe that they do it because they’re happy. They are excited as perhaps they have just met their human companion and caregiver after he has been away for the day. Such greetings and tailwagging go together which gives the impression that the pleasant experience and excitement results in a wagging tail.

This, however, is incorrect according to Dr Desmond Morris who I find completely plausible. He is the expert when it comes to cat and dog behaviour. He states quite definitively that “the only emotional condition which all tail-waggers (both canine and feline) share is a state of conflict”. He goes further: this behaviour is true for almost all animals.
When a dog is in conflict he is pulled in two different directions. He is in two minds. The body is in a state of tension. It begins to move in one direction in response to an urge and then stops and moves in another direction in response to another urge. This results, he says, in dogs performing various movements such as yes, tail-wagging. They might also bob their heads or twist their necks or bend their legs or jiggle their feet.
Tail-wagging must always be interpreted in context of the circumstances. He says that there are two urges in the minds of a dog who is tail-wagging. These are the urges to stay and to go away. The urge to stay comes from the dog’s wish to be friendly, for example, and the urge to go away will be caused by anxiety or fear. The urge to stay may be due to hunger and the urge to stay away will be due to fear.
When puppies feed from their bitch they are hungry and they are drawn to feeding. Anxiety or fear and therefore the urge to stay away comes from the fact that they are feeding with siblings with whom there is rivalry and there may have been some bullying and biting between them.
Tell-wagging occurs when there are sexual advances. There is sexual attraction countermanded by a simultaneous fear. An aggressive dog may be in two minds because there is an urge to be aggressive and an urge to be fearful.
You can see this behaviour in wolves. The reason why tail-wagging has been misunderstood is because of the dog-human greeting when it occurs. People see the submissive dog greeting their master or mistress. The dog feels a mood of friendliness and excitement at seeing their pack leader. That feeling is countermanded with a slight apprehension. This causes the conflict, the dog being in two minds resulting in tail-wagging.
People don’t want to believe that their dog is apprehensive when they are greeted but it happens because of the size difference between the two species. Humans tower over their dogs. This is worrying for them. Add in the fact that humans are dominant over their dogs and you can create mixed feelings in a dog’s mind when being greeted.
Displacement activities, it seem to me, also happen when the mind in conflict. A cat might lick their nose which is a displacement activity. Humans scratch their head or bite their nails as displacement activities.