Gandalf is a Maiar, which means he is a lesser god. He was put on Middle Earth to combat Sauron. He is supposed to help men and elves by spreading knowledge. Although he helped he probably could have done much more but he wasn't there to win he was there to guide.
My point is that he is lesser god and should be able to defeat Palpatine. I don't think Palpatine could take the Balrog.