Williams lashed home a rebound after Shadrach Ogie’s initial effort was blocked to send his side top of the table.
The victory was the hosts’ fifth league win of the season and helped them bounce back from two straight defeats.
The Gills went close early on when Williams fired narrowly over the top from 20 yards.
The hosts continued to press and George Lapslie rose to meet Scott Malone’s cross, only to head inches wide.
Macauley Bonne planted a header off target for Gillingham before Harrogate threatened just before half-time when Kayne Ramsey lashed an effort clear of the crossbar.
After the restart Gillingham goalkeeper Jake Turner produced a smart stop to deny George Thomson.
The hosts replied with Lapslie drilling a low effort off target.
The sweltering conditions appeared to be taking their toll on both teams in the later stages, while there was a lengthy hold-up as Town’s Sam Folarin left the field on a stretcher, but Williams’ instinctive strike won the game six minutes into added time.