Stamkos scored his 29th goal, two behind NHL-leading Alex Ovechkin of Washington, and added an assist.
With 55 points, Toronto is one point ahead of the New York Islanders for fifth place in the Eastern Conference. Both teams, who have already clinched playoff berths, have two games remaining.
St. Louis' goal in the second period came off wrist shot off a slick pass from behind the net by Nate Thompson to give the Lightning a 2-1 lead.
In the third period, with the Lightning leading 3-2, St. Louis got to a rebound of Keith Aulie's slap shot off the boards and slipped the puck past goalie James Reimer.
St. Louis completed his hat trick with an empty-net goal with 2:49 left in the game.
It was his seventh NHL hat trick and first since Feb. 26, 2012, against the New Jersey Devils.
"To see what he does, it doesn't amaze me, but it amazes a lot of people," Stamkos said of St. Louis. "I'm pretty fortunate to be able to see him every day and see how hard he works and how much he wants to be the best player that he can be."
Toronto took a 1-0 lead with 2:57 remaining in the first period when Phil Kessel pushed a wrist shot past goalie Ben Bishop.
Tampa Bay tied it in the second period when Stamkos took an open-ice pass from Thompson and slipped a backhanded shot past Reimer. Joffrey Lupul scored for Toronto with 24 seconds remaining in the second to tie it 2-2.
"We started giving up a few too many chances," said Maple Leafs forward Clarke MacArthur, who assisted on Lupul's goal. "It's not our style to give up the odd-man rushes. We're going to have to clean that up."
Radko Gudas' third-period goal, a slap shot that went over Reimer's shoulder, gave the Lightning a 3-2 lead.
"Our execution with the puck has to get better," Maple Leafs coach Randy Carlyle said. "We can't make these same mistakes in the postseason."
Bishop made 26 saves. ... The scoreboard updated the NHL scoring race after St. Louis' second and third goals. ... St. Louis' hat trick was the first for the Lightning this season. ... Stamkos registered a career-high, plus-5 rating.
Copyright by STATS LLC and The Associated Press
TAMPA, FL - APRIL 24: Nate Thompson #44 of the Tampa Bay Lightning walks out to the ice before the game against...