As a ?Maiar? who has been traveling Middle-Earth for centuries, steeped in the languages and lore of every race and individual he came across, it is granted that Gandalf would have many aquzintances and connections. Middle-Earth being a land of numerous beings, and gandalf's travels being as broad as they were, it is more than likely that, at some point, he would have encountered one of these giants. After learning their language, it is quite possible that Gandalf could have developed a friendly relationship with one or more of these giants. It is also possible that a giant may be indebted to Gandalf, who, being a wizard and all, may have used his wisdom or powers to aid a giant in the past.
Gandalf, therefore, would not have to "command" mountain giants. He would only have to make the request that the giant plug up the whole so that travelers are not enslaved or eaten by the more uncouth inhabitants of the subterranean. Any decent giant would be more than willing to render such a simple, yet nonetheless beneficial, service to the greater population.
Although it is also possible that, during his many escapades, Gandalf may have performed some deed that would have earned him leadership over the mountain giants. This is unlikely, however, because Gandalf seems to have preferred to remain politically neutral so as to prevent the complication of his already complex and numerous duties as a semi-divine guardian of what is just and good.