The Return of the King
We mentioned in Gandalf's The Two Towers "Character Analysis" that he is basically Middle-earth's version of an angel. He's thousands of years old, he's super-powerful, and he fights as a champion for the powers of Good. So if Gandalf is a direct representative of Good on Middle-earth, why doesn't Gandalf just face Sauron mano a mano to see who wins? Why go through this whole battle thing at all?
The most obvious answer, of course, is that there would be no story if Gandalf just quietly went off to face down Sauron as soon as he was reborn as Gandalf the White. We wouldn't need The Two Towers or The Return of the King, which would obviously be a shame. What would we read, then?
Secondly, Gandalf has to let the people of Middle-earth start learning to take care of things on their own. With Gandalf leading their forces, they have some guidance from the powers for Good. But if the Middle-earthlings are ever going to make any real, moral decisions, they have to be the ones to do the real fighting in this war against Sauron. If they managed to defeat Sauron without making any tough calls, how would we know they could stand up to whatever evil pops up down the road?
But perhaps the biggest reason of all that Gandalf cannot just take care of business where Sauron is concerned—the reason he has to let this war play out, even if some good people must die as a result—is that Gandalf has a body. Gandalf isn't just an angel. He is an incarnate angel, which means he has the same limits that anyone else with a body has. Gandalf may be powerful, but he is not actually a god. He cannot be in two places at once, and he has to make some ugly choices as a result.
Just as the Lord of the Nazgûl brings the war right to the gates of Minas Tirith, Pippin pulls Gandalf away from the battlefield to announce that Denethor is trying to set both himself and his son on fire. Gandalf answers:
Maybe I can [save Faramir] … but if I do, then others will die, I fear. Well, I must come, since no other help can reach him. But evil and sorrow will come of this. Even in the heart of our stronghold the Enemy has power to strike us: for his will it is that is at work. (5.7.9)
This moment in the middle of the battle for Minas Tirith is really important because it illustrates exactly what the limits of Gandalf's power are. He often appears all-knowing, and once he comes back from the dead, it's hard to see what he can't do. Won't he always be around to save the day? Unfortunately, not so much. Right here, Gandalf has to choose between facing the Lord of the Nazgûl and saving Faramir. He picks Faramir, and as a result, Théoden dies on the battlefield. Oof. That's gotta hurt. Here is our proof that Gandalf cannot just wave his magic staff and make bad things go away. He may be an angel, but he still has to face reality.
You're On Your Own, Kids
Morally speaking, we think of Gandalf like a pair of training wheels. Sure, this may sound bizarre, but hear us out: Gandalf is to the good people of Middle-earth like training wheels are to someone learning to ride a bike.
The thing about training wheels is, they are hugely reassuring when you are trying to find your balance on two wheels. They are there to support you as you wobble your way down the road on your new bike. Maybe they will help you avoid that particularly deep pothole or large branch in the road. However, you also know deep down that, to become a real cyclist, you will eventually have to find your own balance. Someday, you will have to leave those training wheels behind.
Maybe you see where we are going with this? During the War of the Ring, Gandalf and the older elves such as Elrond keep remembering back to the first days of the war against Sauron. This war has been going on for a long time, longer than the younger peoples of Middle-earth, such as the hobbits or even the humans, can fully remember. And Gandalf, with his wisdom and long experience, offers his support to men like Aragorn and Faramir as they begin fighting this terrible world war. Everyone on the side of Good benefits from Gandalf's advice and assistance.
But once the War of the Ring ends, Gandalf announces that his time in Middle-earth is done. He has taught the people of Middle-earth everything that he can. And the time has come for them to face the challenges of the new Age on their own. Gandalf can't hold their hands forever. To follow our earlier comparison, it is time for the people of Middle-earth to take those (wizardy) training wheels off their collective bikes and start cycling on their own. Okay, maybe it's not a perfect analogy, but we hope you get the idea.
As we mention in "What's Up With the Ending?" Gandalf explains this new arrangement to Merry, Pippin, Frodo, and Sam: "I am not coming to the Shire. You must settle its affairs yourselves; that is what you have been trained for" (6.7.67). The War of the Ring has given these hobbits (and everyone else who fought for Good) the skills they need to sort out future questions of Good and Evil, such as how to deal with Saruman's Shire hooligans, on their own. And even if they make mistakes, from here on out, those are their mistakes to make. Finally, Gandalf is free to sail out West and take a vacation (and honestly, considering that he is thousands of years beyond retirement age, we think he's earned a break).
P.S. We find out at the very end of the novel that Gandalf carries one of the three Elvish Rings of Power: "Narya the Great" (6.9.81), with a fiery red stone. It's not evil, since Sauron didn't make it. And that's why Gandalf gets to carry this Ring with him out of Middle-earth.