When Thomas Was still observing the Maze from the WCKD headquarters observation room, he had found that the other Gladers had gotten over their fear of their new environment, also for the fact that they lost their memory.
When they had declared one of their rules is that:
No one is to set foot out of the Glade unless you are a runner.
But Gally later on in another day wanted to know what it was like to set foot out of the maze...just a little bit.
So he stepped into the maze when no one was looking just outside the glade. But when he did, he wasn't expecting it and he was attacked by a Griever that cam out of nowhere, it rolled all over him.
Gally managed to escape though and get back into the glade where he was found in a delirious state by the other Gladers. Later he went into the three-day coma of what they called the changing. He broke a prime rule in the Glade so then he can experience somehting that he was not allowed to.
This is how he was stung and when he was stung.