The Wildcats had the ball out of bounds under Baylor's basket with 1 second left, and Angel Rodriguez spotted McGruder unguarded on the right wing.
The senior guard received the inbounds pass made the most of the opportunity, canning the long shot as time expired to lift Kansas State (24-5, 13-3 Big 12) to its fifth straight win.
McGruder finished with 18 points as Kansas State remained tied with No. 5 Kansas atop the Big 12 standings.
Baylor (17-12, 8-8) was led by Pierre Jackson with 18 points and seven assists.