You can find the answer to this question right at the start of Chapter 3. It's about one page into that chapter (page 17 in my copy of the book).
Holden Caulfield gets the red hunting hat while he is in New York City on the day that the novel starts (this is when he is in with the fencing team and loses all their stuff). Right after he loses the swords, they get out of the subway and he sees the hat in the window of a sporting goods store.
He likes it and buys it for one dollar.
Holden sees and buys the red hunting hat in New York after losing the fencing equipment for the Pencey Prep team. It is an unusual hat, and Holden likes to wear it backwards. Not only does it keep him warm when he is wandering around New York City, but it is red. The color red is a recurring motif in the novel and includes the color of Allie and Phoebe’s hair. Perhaps this is a reason why Holden wears the hat. It could be a symbol of Allie and Phoebe’s innocence, something Holden is “hunting” for in the phony adult world in which he thinks he is. It can also be a symbol of the childhood he is reluctant to leave behind as he grows into adulthood. It is a quirky choice of hats for anyone not hunting, but Holden’s personality seems to fit the hat perfectly. Holden’s desire to not be conventional probably dictated his choice of hats.
He buys the hat after he looses the fencing team's materials and gets out of the subway. He spots it in a store and buys it for a dollar.