I've always assumed that in ROTJ Yoda just died of old age, but after just watching it again, I just realised that he says something like "soon I will rest, for ever sleep" and then I think he says "wounded I am" So, I guess Yoda gets wounded in ep 3? Maybe he's wounded while fighting Palpatine?
It's "earned it, I have". He's 900 years old, after everything he's been through, he felt that he has earned the chance to put off his mortal coil, so to speak.
He didn't say "wounded I am." He said "earned it, I have". And boy, after all he must have been through during the Clone Wars and watching Palpatine corrupt Anakin... I guess he sorta did.
He probably would have died sooner (maybe during or right after Episode III), but he was keeping himself alive with the Force or sheer willpower to see Luke to his training. He only kept alive long enough after ESB to see Luke in ROTJ and have closure. Check out AOTC. Shmi stayed alive just long enough to see her son. That doesn't even have to do with force abilities, that was all willpower. Intuition told her to hold on.
He did what was needed and that was to train Luke. Yoda busted his whole life for the good of the galaxy, he should get a satisfying death.
I think the clue to this question comes from the first few drafts of Return of the Jedi (or is it Revenge?). In this, Yoda much like Obi-Wan knowingly turns to the Dark Side, aware that he can come back if he wishes and manifests himself as and when Luke needs it. For instance, I recall a scene where the Emperor attacks Luke using Lightning and a vision of Yoda blocks his attack on Luke. I believe that although it isnt seen on screen, Yoda, just like Obi-Wan is helping Luke through the ordeal he is enduring in the Throne Room.