Gray had touchdown runs of 84 yards in the second quarter and 48 yards in the third. Von Miller intercepted a tipped pass at the Texas A&M 11 with 2:37 left to kill what looked like a drive for the tying touchdown.
Jeff Fuller also caught a touchdown pass and Randy Bullock kicked a 50-yard field goal for the Aggies (9-3, 6-2 Big 12).
Texas' fifth home loss of the season means the Longhorns (5-7) won't be eligible for a bowl just one season after playing for the national championship.
The Aggies won their sixth game in a row and still have a chance to win the Big 12 South if Oklahoma beats Oklahoma State on Saturday. The Aggies would still be a long shot for the Big 12 championship game because of a tiebreaker based on BCS standings.
Cody Johnson rushed for 107 yards for Texas. Gilbert was 20 of 37 passing for 219 yards.
Gray is the first Texas A&M running back to reach 200 yards since 1995.
Texas A&M scored 17 points off three Texas turnovers and led 24-14 after Gray's second touchdown. Texas kicked a field goal after a blocked punt and used a long drive to march toward a touchdown before Garrett Gilbert's pass was tipped at the line of scrimmage. Miller, a defensive end who drifted back into coverage, leaped high to grab it.
Texas A&M punted the ball back to Texas with about 10 seconds left and Ken Wood had to pick the snap off the ground to get it away. Texas tried to block it and freshman returner Adrian Phillips let the ball bounce on the ground.
Time ran out in a mistake fitting of Texas' miserable season and several players threw up their hands in frustration.
Texas had never had a losing season under coach Mack Brown but completely fell apart after quarterback Colt McCoy and a host of other players ended their careers. The Longhorns lost five home games.
One season after winning the Big 12, Texas finished dead last in the South division.
The Longhorns' struggles have intensified speculation among fans that Brown will replace one or more assistant coaches, including longtime offensive coordinator Greg Davis. Davis has said he doesn't plan to retire.
The Aggies have made a complete turnaround in coach Mike Sherman's third season. After seasons of 4-8 and 6-7, the Aggies can get their first 10-win season since 1998.
Texas scored first on Gilbert's 31-yard touchdown pass to Marquise Goodwin. Texas A&M cornerback Terrence Frederick bit on a ball fake, leaving Goodwin wide open for his first touchdown of the season.
Gilbert seemed to be gaining confidence with his arm and feet with every play, then made a huge mistake.
Texas recovered a fumbled punt and moved to the Texas A&M 8 before a botched handoff with Johnson caused a fumble. Two plays later, Gray swept to his right, cut back left, and was in the clear for an 84-yard touchdown run.
Gilbert threw an interception on Texas' opening drive of the third. He threw well behind wide receiver James Kirkendoll and Dustin Harris got a hand up to tip the ball to himself on the Texas 38. The Aggies went up 10-7 on a Bullock's field goal.
Fuller's touchdown catch made it 17-7 but the Aggies couldn't find the drive to put the Longhorns away. After another fumbled punt, Gilbert scored on a 1-yard run to pull Texas to 17-14 in the third.
Then Gray ripped off another big run.
And just like before, the Aggies got it right up the middle of the Texas defense. Ryan Tannehill faked a quick pass then turned to hand off to Gray, who took three steps and had open field to the end zone.