This is explained in the episode as they sit outside the pub at the end.
As Sherlock and John prepare to leave the next morning, John wonders why he saw the hound in the lab despite not having inhaled the gas from the hollow. Sherlock surmises that John was poisoned by the leaking pipes in the laboratory, and John realises Sherlock locked him in the labs in order to test his theory. He also points out Sherlock was wrong for once; he believed the drug was in Henry's sugar and put it in John's coffee.
JOHN: Listen: what happened to me in the lab?
(Sherlock looks at him for a moment, then turns around and reaches for a box of sauce sachets, looking worried about how he’s ever going to explain all this.)
SHERLOCK: D’you want some sauce with that?
JOHN: I mean, I hadn’t been to the Hollow, so how come I heard those things in there? Fear and stimulus, you said.
SHERLOCK (rummaging through the box of sachets): You must have been dosed with it elsewhere, when you went to the lab, maybe. You saw those pipes – pretty ancient, leaky as a sieve; and they were carrying the gas, so ... Um, ketchup, was it, or brown ...?
JOHN: Hang on: you thought it was in the sugar.