The story
Attested from the late 19th century in the United States in the sense of “bluff”; the sense “speak without authority or knowledge” developed later. Although some people speculate a connection to a former requirement that British Members of Parliament wear hats, the connection is implausible.
Etymology from Wiktionary, CC BY-SA 4.0.