Jack Sparrow has this thing about his hat. He's kind of overprotective of it. He won't let Will touch it, he insists that the Commodore returns it, etc.
Now, for half the movie, Jack's hat is always on his head, his hand or there is a explanation for why it's not on neither. Fast forward to the scene in the cave, in the parley between Jack and Barbossa. There's the hat.
Fast forward again, to where Jack and Barbossa are speaking on the Pearl's cabin, before the attack. No hat. Seeing as he's not yet a prisoner, it doesn't seem probable that the crew took it. And he wouldn't just leave it lying around.
Then, he's put in the brig. No hat.
Then, during the battle. No hat.
Then, when he's being marooned. They throw Jack's belongings overboard; his jacket, his sword, his pistol. But no hat.
Then, on the island. No hat.
Then, on the Dauntless. No hat.
Then, inside the cave and during the final battle. No hat.
Then, when he's about to be hanged. No hat.
But then, right at the conclusion, he's pulled up by the Black Pearl's crew...and they hand him his hat.
Where did they get it?
I suppose it's possible that it's a different hat. But that's unlikely. It's also possible that he left it on the Pearl and when his crew took it over, they found it lying around somewhere. But that doesn't seem very likely, either (he really likes the hat.)