There is both Scriptural and Traditional basis for it, but it’s quite a long bit of history to dive into. In short it amounts to a conclusion based on a couple of known facts: a) the Church can not teach error (Scripture), b) the Church is defined, in part, through its unity with Peter/the See of Rome (Tradition/Church Fathers/Church Councils), and c) from early times the See of Rome was held as an “appelate court” with jurisdiction to address complaints about local bishops that otherwise would have no place to go (Tradition/Fathers).
From these it’s basically concluded that “where goes Rome, so goes the Church”, because if Rome is the “final court of appeals”, and the Church can’t err, then God must protect the Bishop of Rome from error in those extraordinary circumstances when he must act as guide for the whole Church. The exercise of this “power” is extraordinary, not ordinary, meaning it’s not something that’s tossed around on a daily basis by virtue of the Bishop of Rome being Pope. On the contrary, the man who is Pope has three “roles”: Pope of the Catholic Church, Patriarch of the Latin Church, and Bishop of Rome. My estimation is that 98% of the time he’s operating in the capacity of either of the latter two, rather than the former. The fact is that he just doesn’t have to work as “Pope of the Catholic Church” most of the time (though he must always be concious of the fact that he
is the Pope, and the responsibility that entails), and most Popes wouldn’t WANT to work in that capacity all the time (there aren’t enough hours in a day, or cells in the brain).
In day to day operation of the Church, it’s the Magisterium (the bishops in Communion with Rome, including the Bishop of Rome himself) that operates “infallibly”, because “as go the bishops, so goes the Church”, and the Church can’t err. Even the Magisterium can operate as a whole in extraordinary circumstances, meaning Ecumenical Councils (and this is similar, though not identical, to what occured in the Council of Jerusalem). These are used for most cases of heresy pervading the Church, where the bishops can come together and discuss the Faith and the Truth, and issue rulings as a body about what to do regarding the matter at hand. Since the Pope isn’t the “king of the Church”, he’s bound by Ecumenical Councils as well, with the caveat being that the Pope in office during an Ecumenical Council has “veto power” in the Council (based on what was said above). Once an Ecumenical Council has been set, however, the decisions are binding on the Pope(s) as well.
So we have the “ordinary Magisterium”, which operates infallibly on a day to day basis, and two operations of “extraordinary Magisterium”, which operate in extraordinary circumstances. All rest in the same promise of Christ to the Church, however, that the “gates of Hell will not prevail against it”.
The decree concerning the infallibility of the Pope, and under what circumstances it applies, is found in the
documents of the First Vatican Council (it’s the last Canon just before the footnotes). The documents pertaining to the Magisterium in general are scattered throughout history, but are best summed up in Lumen Gentium, from the Second Vatican Council.
Peace and God bless!