The thieves ran away when they heard the gun going off.
The thieves ran away when they heard the gun go off.
Is it the first one more correct?
They're both correct and acceptable. I would perhaps read one very slight difference between the two:
1) ... the gun going off - this would suggest ongoing gunfire, perhaps from a machine gun or a pistol being fired several times
2) ... the gun go off - this suggests one single gunshot.