Longoria, who tied the game with a solo shot in the eighth inning, slugged a 1-0 pitch from Takashi Saito (2-3) into the left-field seats with Michel Hernandez on third base to give the Rays their fifth win in their last eight games.
Lance Cormier (2-1) won it in relief as Boston's four-game winning streak came to an end.
Both teams had a chance to win the game in the 10th inning, but Dustin Pedroia hit into a double play that ended Boston's threat, and Tampa Bay failed to score with the bases loaded and no outs.
Kevin Youkilis gave Boston a 1-0 lead with a home run in the second inning and Pedroia added a solo homer in the sixth.
The Rays picked up a run in the seventh on Jason Bartlett's single and then tied it 2-2 in the eighth on Longoria's solo shot.
Copyright 2009 by United Press International.