How are the total possible trophies awarded determined?
SuperCell, the game's creator, has provided this resource for those curious about matchmaking and how trophies work.
To summarize, it is completely dependent on your number of trophies in relation to your opponent's. If your opponent has significantly more trophies than you, you will gain a lot more trophies than usual if you win offensively or defensively. The opposite also applies, where if you lose an attack or defense against someone with less trophies than you, you will lose more trophies.
Level of town hall or any other building has no part in the calculation.