The story
In reference to cattle ranchers and the hats they stereotypically wear; also derives from people wearing cowboy hats as fashion, but who are not actually cowboys, i.e., a poser, or a pretentious and phony individual.
Etymology from Wiktionary, CC BY-SA 4.0.