A pope is elected for life unless he willingly and freely chooses to retire or resign (abdicate) his position.
No one can force him to do so, however, since no other bishop, cardinal, or even ecumenical council has more authority than him. If he is sick and incapacitated, there is no “vice pope” to fill in or take over should the current pope die or resign.
Sadly, there have been several bad popes in history, yet their immoral behavior was never promoted or deemed acceptable by the Church. As bad and as evil as some of these scoundrels were (members of the Borgia and Medici families, for example), committing sins of fornication, adultery, murder, theft, greed, and violence, not one of them ever tried to eliminate one of the commandments nor did any of them attempt to convince the faithful that their personal sinfulness was something to imitate.
They may have had few or no scruples, yet they never taught as official doctrine a repeal of any of the moral laws of God. Some had their enemies poisoned; some had illegitimate children. None ever taught that what they did was okay.
Of the 266 popes in history, despite these bad popes, three times as many have been named saints, and the rest are considered reasonable and decent fellows. When a bad pope appears, there is only one remedy: prayer. Catholics must pray that the man repents from his evil and turns back to being good or pray that the good Lord takes him from this earth.
Pastors and bishops are asked to resign when they turn seventy-five, and cardinals are ineligible to vote in conclaves after they turn eighty. There is no such retirement age for a pope. His mission is for life, even when the latter years are filled with illness and suffering, as in the case of Pope John Paul the Great who shepherded the church to his dying breath.