Pope's usually serve until death, but there have been exceptions.
Once a man becomes Pope, he is Pope for life unless he chooses to step down. The current Code of Canon Law specifically provides for a situation where a Pope may wish to step down: “If it happens that the Roman Pontiff resigns his office, it is required for validity that the resignation is made freely and properly manifested but not that it is accepted by anyone.” (Code of Canon Law, 332 §2)
It is the Pope who promulgates the Code of Canon Law, and he would have the ability to step down whether Canon Law provided for it or not.
There are at least four examples of Popes who have stepped down:
The stories of the first two examples are intertwined. Theophylactus of Tusculum became Pope Benedict IX in 1032 at the young age of 20 due to the political influence of his father. In 1045, for a large sum of money, he abdicated and gave the papacy to his godfather, a priest name John Gratian, who became Pope Gregory VI. However, a little over a year later in 1046, a synod of bishops accused him of simony (the sin of buying or selling a Church office, named after Simon Magnus, who tried to buy Holy Orders in Acts 8:18-20), and he stepped down as well. (Mann, Benedict VI, Gregory VI)
The third example: Born to a poor family, Pietro da Morrone became a Benedictine monk at age 17 and was eventually ordained a priest. He lived a highly disciplined life modeled after the asceticism of St. John the Baptist for many years. After the twelve cardinals charged with the task of electing a new pope following the death of Pope Nicholas IV had been unable to decide on a candidate for more than two years, Morrone sent them a message that God would send a severe chastisement on the Church if they did not pick a new pope in four months. Moved to action, the cardinals unanimously elected Morrone as pope. Morrone accepted, was consecrated a bishop (since he was only a priest), and became Pope Celestine V in July of 1294. However, within five months, and after a string of poor management decisions, he became convinced that God was calling him back to his previous life as a hermit. Since there was still some ambiguity in the minds of some in the Church of whether a Pope could legitimately step down, he issued a decree definitively declaring that a Pope could step down, and then stepped down in December of 1294. His immediate successor, Pope Boniface VIII, worried that Celestine V could be a threat to his power. Boniface had Celestine jailed, where the latter eventually died. A few years later, following the death of Boniface, Celestine was canonized a saint by Pope Clement V. (Loughlin)
The last time a Pope stepped down was in the 15th century. When Gregory XII was elected Pope following the death of Innocent VII in 1406, there was another person claiming to be Pope in Avignon, France. Though claiming the papacy falsely, the French antipope was causing a great deal of confusion in the Church. When he was elected, Gregory XII agreed that he would resign the papacy if the French antipope agreed to do so as well, while at the same time allowing the Church to elect a new Pope. After several years of continued impasse, there appeared a second antipope. The mess was finally settled when Pope Gregory XII authorized the Council of Constance and resigned. The council successfully deposed both antipopes and elected a new Pope, Martin V (Ott).
Code of Canon Law, 332 §2 http://www.vatican.va/archive/ENG1104/__P16.HTM Loughlin, J. (1908). Pope St. Celestine V. In The Catholic Encyclopedia. New York: Robert Appleton Company. Retrieved November 28, 2012 from New Advent: http://www.newadvent.org/cathen/03479b.htm
Mann, H. (1907). Pope Benedict IX. In The Catholic Encyclopedia. New York: Robert Appleton Company. Retrieved November 28, 2012 from New Advent: http://www.newadvent.org/cathen/02429a.htm Mann, H. (1909). Pope Gregory VI. In The Catholic Encyclopedia. New York: Robert Appleton Company. Retrieved November 28, 2012 from New Advent: http://www.newadvent.org/cathen/06791a.htm
Ott, M. (1910). Pope Gregory XII. In The Catholic Encyclopedia. New York: Robert Appleton Company. Retrieved November 28, 2012 from New Advent: http://www.newadvent.org/cathen/07001a.htm