The Dons twice led in the first half, through David Fitzpatrick's 20-yard strike and Tom Elliott's volley.
But Compton levelled on both occasions, running through to slide home and curling in a free-kick from out wide.
And the winger sealed his treble from the penalty spot after substitute Kevin Dawson was fouled by Callum Kennedy.
The home side drop to 10th in the table after seeing their four-game winning run come to an end, while the Glovers are three points ahead of Dagenham & Redbridge, whose match was postponed.
AFC Wimbledon manager Neal Ardley told BBC Radio London:
Media playback is not supported on this device
"You concede three goals at home, that's not good enough.
"I knew this would be tough and I drummed it into the players. That's the sort of game I expected.
"We struggled at times and we had too many players who probably didn't perform to the levels they have performed at. We're giving away really, really poor goals.
"We ran out of ideas trying to chase the game in the last 20 minutes."

The Reds had failed to score more than once in their previous 10 League Two games, but were dominant against Warren Feeney's side.
County striker Scott Boden opened the scoring at the wrong end when his header beat Joe Day after 36 minutes.
Shamir Fenelon then headed home eight minutes after the break, before Newport's Medy Elito was sent off with five minutes left for two yellow cards.
"It was disappointing, I don't think we got going from the first whistle, we couldn't get into a rhythm and our passing was poor," Newport manager Feeney told BBC Radio Wales.
"We have to put it to bed and move on to the next game, we are still in a fantastic position to avoid relegation and that is still my aim.
"You have ups and downs in a season and this was a down, the worst performance since I have been the manager. No-one likes to get beaten."
Jack Compton hit a hat-trick to earn a shock comeback win at AFC Wimbledon and take Yeovil Town three points clear of the League Two relegation zone.
[NEXT_CONCEPT]
Crawley completed a double over Newport County with a comfortable 2-0 victory.