The easiest way to determine the mood of your cat is to watch their tail. A contented or confident cat will walk with their tail held high and raise it when you stroke their back. If they are lying down, the tail may twitch a little but will largely remain still. A trembling tail (especially if combined with a desire to rub their face and chin against you) is a sign of great affection and happiness. If your cat is holding his tail high and then flicks the end of his tail, he is acknowledging you but is otherwise engaged and too busy to stop for a chat.
When a cat holds his tail diagonally up from his body, he is less happy and possibly feeling nervous, while a tail held low is a sign of displeasure or fear. If his tail is held upright, but the end is deflected off to one side, he may be friendly towards you, but unsure of whether you are friendly towards him, whereas if his tail sticks horizontally out from his body but the end droops down, he may be feeling defensive, or even preparing to attack you.
Many cats twitch the ends of their tails when hunting or playing, but if the twitching becomes more violent it is likely that the cat is becoming angry or unsettled. If you are stroking your cat and his tail twitches violently or thumps, then he is becoming annoyed and is not enjoying your attention; you should probably leave him alone for a while.
If your cat puffs his tail up, he is feeling threatened or has had a shock and is trying to make himself look bigger in order to get you (or another animal) to leave him alone. He may also turn his body side on and the hair on his back may stand on end.
Some cats, such as the Manx, do not have a tail. However, they will move the small stump that they do have around as if it was a tail, so you can still determine their mood by paying close attention.