The Rays were trailing 2-0 in the sixth when they broke through against Yankees starter CC Sabathia.
Sam Fuld collected a leadoff single and Desmond Jennings followed with a walk, bringing up Ben Zobrist, who hammered a pitch into the left-centerfield gap for a score-tying two-run double.
Longoria came up next and sent a liner to left, scoring Zobrist with the game's key run.
The Rays' third baseman led off the eighth with a solo homer for an insurance run as Tampa Bay kept pace with the Boston Red Sox atop the American League Central with its eighth win in 10 games.
David Price (8-5) went six innings to get the decision, allowing two runs on five hits. He walked one and struck out five.
Sabathia (11-11) ended up allowing three runs on six hits over 6 1/3 innings for the Yankees, who fell 4 1/2 games out of an AL wild-card spot.
Ichiro Suzuki and Austin Romine each drove in a run for New York.