They fled Moria because of the Balrog. Smaug sacked Erebor, not Moria.
Erebor, or the Lonely Mountain, was the dwarven kingdom which was attacked by Smaug and later reclaimed by the dwarves. The Hobbit covers these events.
Moria, beneath the Misty Mountains, was a much larger dwarven kingdom. It was established by Durin I and thrived for many years until, during the Third Age, the dwarves delved too deep and awoke the Balrog, which killed Durin VI and caused the dwarves to flee Moria. This is essentially the answer to your question: the dwarves fled Moria the first time because of the Balrog.
As for how Azog and the orcs could live there in the presence of the Balrog, the obvious answer is because orcs and Balrogs are on the same side. The Balrog isn't a force of utter destruction; it's a creation of Morgoth, and capable of living together with other creations of Morgoth or Sauron such as orcs. Which isn't to say that the orcs and the Balrog were like a happy family together for those hundreds of years after the dwarves fled: only that destroying all the orcs wasn't on the Balrog's to-do list, so it went back to sleep, and the orcs had enough sense not to go bothering it.
See the following map (click for full resolution):
Here's an approximate timeline of the history of both Moria and Erebor:
- First Age: Kingdom of Moria founded by Durin the Deathless.
- T.A. 1980: Balrog awakes in Moria, slays Durin VI.
- T.A. 1981: after further Balrog destruction, the dwarves flee Moria.
- T.A. 1999: Kingdom of Erebor founded by Thrain I, one of the Moria evacuees.
- T.A. 2770: Smaug sacks Erebor and claims it for himself.
- T.A. 2941: Thorin Oakenshield reclaims Erebor. Battle of the Five Armies. The events of The Hobbit.
- T.A. 2989: Balin leads his ill-fated expedition to Moria, hoping to re-establish a dwarven foothold there.
- T.A. 2994: Balin's party slain by orcs.
- T.A. 3019: the Fellowship of the Ring enters Moria. Gandalf slays the Balrog.