Amazingly enough, it's called 'The Doctrine of Papal Infallibility'. The essence always seemed to me is that your pope is a direct conduit from God, and therefore everything he says is essentially as if the word of God. And if God is perfect - which is a principle the christian faith kind of goes with - then the word of the Pope must be perfect.
As you say, this kind of starts to be an issue when Popes start contradicting each other. But then again, faith is nothing if not willing to bend logic to fit into the world view it wants to have.