You buy it. The pirate hat is an item in the game's shop rather than a drop or a reward.
Save up the currency, open the shop, and pick it up from the cosmetics section. Once purchased it moves straight into your inventory, ready to equip.