From St. Thomas Aquinas, Summa Theolgiae, Suppl., 89, 7:
The judgment as regards the sentencing to punishment for sin concerns all the wicked, whereas the judgment as regards the discussion of merits concerns only believers. Because in unbelievers the foundation of faith is lacking, without which all subsequent works are deprived of the perfection of a right intention, so that in them there is no admixture of good and evil works or merits requiring discussion. But believers in whom the foundation of faith remains, have at least a praiseworthy act of faith, which though it is not meritorious without charity, yet is in itself directed to merit, and consequently they will be subjected to the discussion of merits. Consequently, believers who were at least counted as citizens of the City of God will be judged as citizens, and sentence of death will not be passed on them without a discussion of their merits; whereas unbelievers will be condemned as foes, who are wont among men to be exterminated without their merits being discussed.
Roman Catholic teaching has been characterised as “works salvation” but this shows that isn’t the case. Faith–and love (charity)–are necessary prerequisites for whatever we do to amount to anything.
The problem with the medieval construct was that the Church–and to some extent Aquinas–got bogged down in the business of merits. Had they de-emphasised this, they would have avoided the business of indulgences, which in turn detonated the Reformation.