Vatican II explained the doctrine of infallibility as follows: "Although the individual bishops do not enjoy the prerogative of infallibility, they can nevertheless proclaim Christs doctrine infallibly. This is so, even when they are dispersed around the world, provided that while maintaining the bond of unity among themselves and with Peters successor, and while teaching authentically on a matter of faith or morals, they concur in a single viewpoint as the one which must be held conclusively. This authority is even more clearly verified when, gathered together in an ecumenical council, they are teachers and judges of faith and morals for the universal Church. Their definitions must then be adhered to with the submission of faith" (Lumen Gentium 25).
Infallibility belongs in a special way to the pope as head of the bishops (Matt. 16:1719; John 21:1517). As Vatican II remarked, it is a charism the pope "enjoys in virtue of his office, when, as the supreme shepherd and teacher of all the faithful, who confirms his brethren in their faith (Luke 22:32), he proclaims by a definitive act some doctrine of faith or morals. Therefore his definitions, of themselves, and not from the consent of the Church, are justly held irreformable, for they are pronounced with the assistance of the Holy Spirit, an assistance promised to him in blessed Peter."
The infallibility of the pope is not a doctrine that suddenly appeared in Church teaching; rather, it is a doctrine which was implicit in the early Church. It is only our understanding of infallibility which has developed and been more clearly understood over time. In fact, the doctrine of infallibility is implicit in these Petrine texts: John 21:1517 ("Feed my sheep . . . "), Luke 22:32 ("I have prayed for you that your faith may not fail"), and Matthew 16:18 ("You are Peter . . . ").
You are Peter, and upon you as upon a rock will I build my church and the gates of hell shall not prevail against it.
Blessings and peace to his Holiness and all here!