The Cougars (27-7), winners of 11 of their last 13 games, move on to face either Michigan or Montana on Saturday in the second round of the West Regional.
The Aztecs (22-11), who rallied from a 13-point second-half deficit to draw even on two occasions in the final 1:08, completed the 12th 20-win season in their last 13.
Gray's game-winning basket came after San Diego State's Devin Watson had nailed his second consecutive, game-tying three-pointer with 36 seconds remaining.
Gray, the only Cougar to score in double figures, had given Houston a 65-62 lead 13 seconds earlier with a three-pointer.
That came after Watson had buried another three-pointer to complete a four-point San Diego State burst, which tied the game at 62 with 1:08 remaining.
The teams had combined to make just six three-pointers before Watson and Gray combined for three in the final 70 seconds.
Gray's career-best 39 points came on 12-for-25 shooting overall and 4-for-6 on three-pointers. He also connected on 11-of-15 free throws, leading to a career-high scoring night, beating his previous best of 37 set in the season opener against Drexel.
Gray added eight rebounds for the Cougars, who were coming off a one-point loss to Cincinnati in the American Athletic Conference Tournament finals.
Devin Davis had nine points and nine rebounds, and Corey Davis Jr. added nine points for Houston.
Jalen McDaniels scored a team-high 18 points and added nine rebounds for the Aztecs, who entered the contest on a nine-game winning streak, including three straight wins in the Mountain West Conference Tournament.
Malik Pope recorded an 11-point, 11-rebound double-double for San Diego State, and Trey Kell had 12 points and eight rebounds.
Facing Houston for the first time since 2003, San Diego State hung within 31-28 before the Cougars ran off an 8-1 spurt to close the half with a 39-29 advantage.
Houston's biggest lead came at 47-34 with 15:40 remaining in the game.
--Field Level Media
San Diego St. | Houston | |
Jalen McDaniels 18 | Scoring | Rob Gray 39 |
Trey Kell 4 | Assists | Corey Davis Jr. 2 |
Malik Pope 11 | Rebounds | Devin Davis 9 |
Jalen McDaniels 9 | Free Throws Made | Rob Gray 11 |
Jeremy Hemsley 2 | Steals | Corey Davis Jr. 1 |
Malik Pope 3 | Blocks | Armoni Brooks 1 |
Team | Points | FG% | 3PM-3PA | FTM-FTA | Assists | Rebounds | Blocks | Steals | Turnovers |
San Diego St. | 65 | 37.0 | 4-16 | 21-35 | 11 | 39 | 5 | 4 | 12 |
Houston | 67 | 37.3 | 5-13 | 18-24 | 5 | 36 | 1 | 3 | 7 |