Tampa Bay was protecting a 3-2 lead heading into the seventh, during which the Rays scored three times.
Longoria singled home the first run of the inning and then scored along with James Loney on a double by Wil Myers.
Desmond Jennings put Tampa Bay in front for good with a solo homer in the first and Loney added a sacrifice fly later in the inning.
Jeremy Hellickson (5-3) went six innings to get the win, giving up two runs on seven hits without a walk. Four relievers combined to hold Boston without a hit over the final three frames.
Ryan Dempster (4-8) endured the loss after giving up three runs on eight hits in six innings.
Tampa Bay snapped a three-game losing streak.
Millions of Getty images now available for free via embed tool
Ray Liotta sues skin care company over use of likeness