New Mexico was rolling to what appeared to be an easy victory when it held a 70-55 lead with less than 8 minutes remaining.
The Lobos, however, went scoreless for more than 5 minutes and the Trojans climbed within 70-65.
Williams then hit his key 3-pointer with 1:52 to play and the Trojans could score just two more points the rest of the way.
New Mexico has won all of its nine games this season while USC dropped to 3-5.
Hugh Greenwood led the Lobos with 17 points while both Williams and Alex Kirk had 13. New Mexico hit 50 percent of its field goal attempts and was 11-of-24 from 3-point range.
Eric Wise scored 14 points for USC, which placed all five of its starters in double figures.
Moore to attend retreat in to avoid Kutcher's wedding
'How to Train Your Dragon 2' releases 5-minute clip