Let me first provide a brief summary of the doctrine in my own words, and then I'll paste some quotations and links to show the foundations of my summary. I'm not going to try to incorporate every single detail that could be elaborated upon in my summary, but only summarize the main body of the idea of infallibility in the church.
The teaching authority of the Catholic Church resides in the "Magisterium," which is simply the body of bishops who govern the church in communion with the Bishop of Rome, the Pope. God has given what I'll call the "gift of reliability" to the teachers of the church, so that what they teach in terms of the doctrine of the church (whether of "faith" or "morals") is accurate and does not lead into error. This gift is not given to individual bishops acting alone, but only to the body of bishops as a whole--so it is possible that individual bishops, or even bishops in groups smaller than the whole of the body of bishops, might teach error, but the body of bishops as a whole can never do so. Also, the Pope, as the head of the church, has the gift of reliability given to him in his own unique office as well, so that he can never teach error when he is exercising his teaching office.
Sometimes the church teaches a doctrine definitively--that is, it teaches a doctrine as certainly and irrevocably the correct opinion. This might happen when the bishops come together in an ecumenical council and make definitive decrees or statements, or it might happen as all the bishops in the ordinary exercise of their office agree in teaching a doctrine definitively throughout the world. The Pope might teach a doctrine definitively either by formally defining a doctrine as a dogma (this is the famed ex cathedra declaration) or simply by affirming that a doctrine is the definitive teaching of the church. When the church teaches something definitively, since it has the gift of reliability, Catholics are obligated to receive and accept it definitively. Sometimes, however, the church might teach a doctrine non-definitively--that is, it might teach a doctrine in such a way that it is claimed to be true, or accurate, or good to believe or hold or practice, etc., but not in such a way that it is claimed that the final, unchangeable word on the subject has been given. The doctrine is not claimed as definitely certain or true or unchangeable in its current form. For example, the bishops or the Pope might say, "X is the best way to think about this right now," or "We should think X right now," or "So far as we can see at this point, X appears to be true," or "We should do things in this way right now," etc. There could be lots of ways such a non-definitive teaching could be given and a variety of degrees of certainty in such pronouncements--context would determine how to interpret any particular statement or teaching. A non-definitive teaching must be accepted and adhered to by Catholics as well. It must be accepted in the way and to the degree it was intended by the church--again, interpreted by context.
The short of it is that what the Magisterium of the Catholic Church teaches is to be received as reliable and followed accordingly. We can see that one popular idea about the church's infallibility is quite wrong--the idea that Catholics are only obliged to accept a teaching of the church if it is proposed by a Pope in a definitive, formal, ex cathedra statement. Catholics are not at liberty to ignore or reject any teaching on doctrine or morality given to them by the church.
Quotations and References
One of the most complete and helpful statements on this subject given by the church recently is found in the Vatican II document Lumen Gentium, section 25:
25. Among the principal duties of bishops the preaching of the Gospel occupies an eminent place.(39*) For bishops are preachers of the faith, who lead new disciples to Christ, and they are authentic teachers, that is, teachers endowed with the authority of Christ, who preach to the people committed to them the faith they must believe and put into practice, and by the light of the Holy Spirit illustrate that faith. They bring forth from the treasury of Revelation new things and old,(164) making it bear fruit and vigilantly warding off any errors that threaten their flock.(165) Bishops, teaching in communion with the Roman Pontiff, are to be respected by all as witnesses to divine and Catholic truth. In matters of faith and morals, the bishops speak in the name of Christ and the faithful are to accept their teaching and adhere to it with a religious assent. This religious submission of mind and will must be shown in a special way to the authentic magisterium of the Roman Pontiff, even when he is not speaking ex cathedra; that is, it must be shown in such a way that his supreme magisterium is acknowledged with reverence, the judgments made by him are sincerely adhered to, according to his manifest mind and will. His mind and will in the matter may be known either from the character of the documents, from his frequent repetition of the same doctrine, or from his manner of speaking.
Although the individual bishops do not enjoy the prerogative of infallibility, they nevertheless proclaim Christ's doctrine infallibly whenever, even though dispersed through the world, but still maintaining the bond of communion among themselves and with the successor of Peter, and authentically teaching matters of faith and morals, they are in agreement on one position as definitively to be held.(40*) This 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, whose definitions must be adhered to with the submission of faith.(41*)
And this infallibility with which the Divine Redeemer willed His Church to be endowed in defining doctrine of faith and morals, extends as far as the deposit of Revelation extends, which must be religiously guarded and faithfully expounded. And this is the infallibility which the Roman Pontiff, the head of the college of bishops, enjoys in virtue of his office, when, as the supreme shepherd and teacher of all the faithful, who confirms his brethren in their faith,(166) by a definitive act he proclaims a doctrine of faith or morals.(42*) And therefore his definitions, of themselves, and not from the consent of the Church, are justly styled irreformable, since they are pronounced with the assistance of the Holy Spirit, promised to him in blessed Peter, and therefore they need no approval of others, nor do they allow an appeal to any other judgment. For then the Roman Pontiff is not pronouncing judgment as a private person, but as the supreme teacher of the universal Church, in whom the charism of infallibility of the Church itself is individually present, he is expounding or defending a doctrine of Catholic faith.(43*) The infallibility promised to the Church resides also in the body of Bishops, when that body exercises the supreme magisterium with the successor of Peter. To these definitions the assent of the Church can never be wanting, on account of the activity of that same Holy Spirit, by which the whole flock of Christ is preserved and progresses in unity of faith.(44*)
But when either the Roman Pontiff or the Body of Bishops together with him defines a judgment, they pronounce it in accordance with Revelation itself, which all are obliged to abide by and be in conformity with, that is, the Revelation which as written or orally handed down is transmitted in its entirety through the legitimate succession of bishops and especially in care of the Roman Pontiff himself, and which under the guiding light of the Spirit of truth is religiously preserved and faithfully expounded in the Church.(45*) The Roman Pontiff and the bishops, in view of their office and the importance of the matter, by fitting means diligently strive to inquire properly into that revelation and to give apt expression to its contents;(46*) but a new public revelation they do not accept as pertaining to the divine deposit of faith.(47*)
This is from the Catechism of the Catholic Church:
888 Bishops, with priests as co-workers, have as their first task "to preach the Gospel of God to all men," in keeping with the Lord's command.415 They are "heralds of faith, who draw new disciples to Christ; they are authentic teachers" of the apostolic faith "endowed with the authority of Christ."416
889 In order to preserve the Church in the purity of the faith handed on by the apostles, Christ who is the Truth willed to confer on her a share in his own infallibility. By a "supernatural sense of faith" the People of God, under the guidance of the Church's living Magisterium, "unfailingly adheres to this faith."417
890 The mission of the Magisterium is linked to the definitive nature of the covenant established by God with his people in Christ. It is this Magisterium's task to preserve God's people from deviations and defections and to guarantee them the objective possibility of professing the true faith without error. Thus, the pastoral duty of the Magisterium is aimed at seeing to it that the People of God abides in the truth that liberates. To fulfill this service, Christ endowed the Church's shepherds with the charism of infallibility in matters of faith and morals. The exercise of this charism takes several forms:
891 "The Roman Pontiff, head of the college of bishops, enjoys this infallibility in virtue of his office, when, as supreme pastor and teacher of all the faithful - who confirms his brethren in the faith he proclaims by a definitive act a doctrine pertaining to faith or morals. . . . The infallibility promised to the Church is also present in the body of bishops when, together with Peter's successor, they exercise the supreme Magisterium," above all in an Ecumenical Council.418 When the Church through its supreme Magisterium proposes a doctrine "for belief as being divinely revealed,"419 and as the teaching of Christ, the definitions "must be adhered to with the obedience of faith."420 This infallibility extends as far as the deposit of divine Revelation itself.421
892 Divine assistance is also given to the successors of the apostles, teaching in communion with the successor of Peter, and, in a particular way, to the bishop of Rome, pastor of the whole Church, when, without arriving at an infallible definition and without pronouncing in a "definitive manner," they propose in the exercise of the ordinary Magisterium a teaching that leads to better understanding of Revelation in matters of faith and morals. To this ordinary teaching the faithful "are to adhere to it with religious assent"422 which, though distinct from the assent of faith, is nonetheless an extension of it.
In the document Humani Generis, section 20, Pope Pius XII warns against those who would ignore the ordinary teaching authority of the church:
20. Nor must it be thought that what is expounded in Encyclical Letters does not of itself demand consent, since in writing such Letters the Popes do not exercise the supreme power of their Teaching Authority. For these matters are taught with the ordinary teaching authority, of which it is true to say: "He who heareth you, heareth me"; and generally what is expounded and inculcated in Encyclical Letters already for other reasons appertains to Catholic doctrine. But if the Supreme Pontiffs in their official documents purposely pass judgment on a matter up to that time under dispute, it is obvious that that matter, according to the mind and will of the Pontiffs, cannot be any longer considered a question open to discussion among theologians.
Vatican I makes the same point (Session 3, Chapter 3, Section 8):
Wherefore, by divine and Catholic faith all those things are to be believed which are contained in the word of God as found in Scripture and tradition, and which are proposed by the Church as matters to be believed as divinely revealed, whether by her solemn judgment or in her ordinary and universal magisterium.
Further helpful information regarding the church's understanding of her own infallibility can be found in this document, written up by the Congregation for the Doctrine of the Faith, which contains further elaborations on what sort of assent is required to the teachings of the Catholic Church. Fr. John Trigilio has provided a helpful summary of the various categories mentioned in the document here.
Finally, this article provides text from some documents which show how the infallibility of the ordinary universal magisterium of the church has been exercised in practice regarding the issue of women's ordination.
An Addendum Regarding Galileo
One historical objection that is sometimes raised against the doctrine of the infallibility of the church is that the doctrine is proven false by the church's condemnation in the seventeenth century of the views of Galileo regarding the motion of the earth around the sun. A look at this issue is useful not only for the defense of the Catholic doctrine but also for a better understanding of how infallibility functions (or doesn't function, as the case may sometimes be) in practice. Much can be said and has been said on the Galileo affair, so I will only make some brief comments.
The short of it is that the church was not exercising its infallible teaching authority in the condemnation of Galileo. The condemnation was enacted by a judicial office of the church in a judicial manner, and such an office does not have the authority to define doctrine for the Catholic Church. The Pope ratified the decision of the office, but a ratification of a judicial act by a judicial court does not imply an intention to teach a doctrine to the entire church in a definitive manner. Judicial proceedings are intended to judge particular cases on the basis of already established or assumed rules, not define new rules. An article on the Catholic Answers website puts it this way:
The Church has never claimed ordinary tribunals, such as the one that judged Galileo, to be infallible. Church tribunals have disciplinary and juridical authority only; neither they nor their decisions are infallible.
No ecumenical council met concerning Galileo, and the pope was not at the center of the discussions, which were handled by the Holy Office. When the Holy Office finished its work, Urban VIII ratified its verdict, but did not attempt to engage infallibility.
Three conditions must be met for a pope to exercise the charism of infallibility: (1) he must speak in his official capacity as the successor of Peter; (2) he must speak on a matter of faith or morals; and (3) he must solemnly define the doctrine as one that must be held by all the faithful.
In Galileo’s case, the second and third conditions were not present, and possibly not even the first. Catholic theology has never claimed that a mere papal ratification of a tribunal decree is an exercise of infallibility. It is a straw man argument to represent the Catholic Church as having infallibly defined a scientific theory that turned out to be false. The strongest claim that can be made is that the Church of Galileo’s day issued a non-infallible disciplinary ruling concerning a scientist who was advocating a new and still-unproved theory and demanding that the Church change its understanding of Scripture to fit his.
The Catholic Encyclopedia says this on the subject (embedded links removed):
Can it be said that either Paul V or Urban VIII so committed himself to the doctrine of geocentricism as to impose it upon the Church as an article of faith, and so to teach as pope what is now acknowledged to be untrue? That both these pontiffs were convinced anti-Copernicans cannot be doubted, nor that they believed the Copernican system to be unscriptural and desired its suppression. The question is, however, whether either of them condemned the doctrine ex cathedra. This, it is clear, they never did. As to the decree of 1616, we have seen that it was issued by the Congregation of the Index, which can raise no difficulty in regard of infallibility, this tribunal being absolutely incompetent to make a dogmatic decree. Nor is the case altered by the fact that the pope approved the Congregation's decision in forma communi, that is to say, to the extent needful for the purpose intended, namely to prohibit the circulation of writings which were judged harmful. The pope and his assessors may have been wrong in such a judgment, but this does not alter the character of the pronouncement, or convert it into a decree ex cathedra.
As to the second trial in 1633, this was concerned not so much with the doctrine as with the person of Galileo, and his manifest breach of contract in not abstaining from the active propaganda of Copernican doctrines. The sentence, passed upon him in consequence, clearly implied a condemnation of Copernicanism, but it made no formal decree on the subject, and did not receive the pope's signature.
The nearly unanimous consensus at the time, and throughout history up to that point, was that the biblical statements regarding the fixedness of the earth implied that the earth doesn't move, and when anyone thought about this in any kind of academic way, they generally interpreted the verses in light of the received Aristotelian cosmology. But the Magisterium of the church had never declared definitively that this cosmology must be right, or that all other conceivable cosmologies must be wrong, or that the Bible could never under any circumstances be interpreted according to some other cosmology. In reality, the question never really came up, because no one was questioning the assumed cosmology. So the church, without defining against hypothetical alternatives that might be raised in the future, simply assumed the view of things received at the time. The court that condemned Galileo likewise simply judged him according to the received view of the time without there being involved any intention of the Magisterium or the Pope to define an infallible doctrine on the subject for the whole church. As the Catholic Encyclopedia mentioned, the Pope didn't even sign the sentence of 1633, and, interestingly, three of the ten cardinals who were acting as judges in the case didn't sign it either. So the decision, in addition to being judicial in form and not a doctrinal definition, apparently did not even have the unanimous support of all ten of the judges on the court!
That there was no assumption at the time that the opinion of the court regarding heliocentrism's relationship to Scripture was to be considered the definitive doctrine of the church is illustrated by a letter written by Cardinal Bellarmine, who was closely involved in the earlier part of the affair. In 1615, Cardinal Bellarmine wrote a letter to Paolo Foscarini, who had been putting forward some heliocentric arguments. In the letter he gives his thoughts on the matter:
Second, I say that, as you know, the Council [of Trent] prohibits interpreting Scripture against the common consensus of the Holy Fathers; and if Your Paternity wants to read not only the Holy Fathers, but also the modern commentaries on Genesis, the Psalms, Ecclesiastes, and Joshua, you will find all agreeing in the literal interpretation that the sun is in heaven and turns around the earth with great speed, and that the earth is very far from heaven and sits motionless at the center of the world. Consider now, with your sense of prudence, whether the church can tolerate giving Scripture a meaning contrary to the Holy Fathers and to all the Greek and Latin commentators. . . .
Third, I say that if there were a true demonstration that the sun is at the center of the world and the earth in the third heaven, and that the sun does not circle the earth but the earth circles the sun, then one would have to proceed with great care in explaining the Scriptures that appear contrary; and say rather that we do not understand them than that what is demonstrated is false. But I will not believe that there is such a demonstration, until it is shown me. Nor is it the same to demonstrate that by supposing the sun to be at the center and the earth in heaven one can save the appearances, and to demonstrate that in truth the sun is at the center and the earth in the heaven; for I believe the first demonstration may be available, but I have very great doubts about the second, and in case of doubt one must not abandon the Holy Scripture as interpreted by the Holy Fathers.
Notice that while Cardinal Bellarmine is pretty confident that the common understanding of the Scriptures is correct, he does not consider the matter to be certain, and he acknowledges the hypothetical possibility that someone might at some point prove that the earth moves around the sun, in which case the proper move would be to accept that truth and correct the prevailing assumptions about how to interpret the relevant Scriptures. Nothing in the church's dealings with Galileo ever suggests that Bellarmine's way of looking at the subject was incorrect, and no doubt it was widely held. But Bellarmine makes the point that until more conclusive scientific proof is obtained and recognized, it would be inappropriate to reinterpret the Scriptures away from the consensus assumptions of the Fathers. And that is basically the position Galileo's judges took. In short, he was condemned because he took it upon himself to propose Scriptural interpretations contrary to the received (though not infallibly, definitively defined) assumptions of the church without what his judges regarded as adequate warrant.
Here is another helpful article on the Galileo affair which sums up much of what I've been saying and which also gives a helpful brief history of the affair.
ADDENDUM 5/25/16: The Wikipedia article on this is not bad (at least as it is on this date).