The Phrase Finder says that the expression means someone is talking nonsense, and that they are pretending to have knowledge on a subject on which they are ignorant. Reference is given to Farmer and Henley Slang and Its Analogues, 1888: “Dis is only a bluff dey’re makin’ – see! Dey’re talkin’ tru dere hats.” The possibility is raised that the expression originated from the practice in the UK parliament where you had to be seated and wearing a top hat to raise a point of order. This is followed by “topping,” or “talking out a bill,” which means filibustering with rambling nonsense. However, it is pointed out that stories about top-hatted members of the UK parliament becoming a dominantly U.S. expression doesn’t seem to be likely. There is speculation the idiom originated from men holding their hats over their faces while pretending to pray. I think it is more likely that it is a variation of “talking off the top of your head,” where an empty hat would set, which means you are speaking speculatively without much knowledge.