Why do dogs wag their tails?

I realize, of course, that it’s a reflection of their mood. But why is their mood attached to their tail? Is it a conscious decision, or an involuntary reflex? Is it a form of communication with other dogs/animals?

I once heard a comedian say that the concept of dogs playing poker made no sense, because if a dog got a good hand, his tale would wag. Is this true (not literally, of course)? In other words, could a dog be happy and not wag his tale? Could a dog wag his tale “sarcastically”?

Why Do … Dogs Wag Their Tails?