The Return of the King
How we cite our quotes:
"But," said Sam, and tears started in his eyes, "I thought you were going to enjoy the Shire, too, for years and years, after all you have done."
"So I thought too, once. But I have been too deeply hurt, Sam. I tried to save the Shire, and it has been saved, but not for me. It must often be so, Sam, when things are in danger: some one has to give them up, lose them, so that others may keep them. But you are my heir: all that I had and might have had I leave to you. […] And that will keep you as busy and happy as anyone can be, as long as your part of the Story goes on." (6.9.75-6)
We guess that, if Frodo had decided to complete his quest because he wanted to enjoy the Shire, that would be a selfish (though understandable) motive. The fact that Frodo carries the Ring to save the Shire even though he doesn't get to live there to enjoy it is where the moral lesson comes in. To use a trite expression, Frodo loves the Shire (and its inhabitants) enough to let it go.