In order to understand the Church’s teaching, it is important to understand exactly what she means by grace (and specifically “sanctifying grace,” which is the fundamental condition for salvation).
What is grace?
Grace can refer to any gratuitous gift from God; however the kind that interests us here is called sanctifying or habitual grace. What it consists in is the indwelling of God in a person's soul, sometimes called (especially among the Greek Fathers) divinization or theosis. It renders a person pleasing to God, makes us sons and daughters of God, and makes us capable of supernatural love for God. When one has this indwelling, we say that the person is “in the state of grace.” (See the section on grace in the Catechism of the Catholic Church [CCC] nos. 1996-2005.)
The Biblical basis for this doctrine
The Bible, of course, does not use this technical theological terminology, but there are references to the underlying reality in the Scriptures. For example,
His divine power has granted to us all things that pertain to life and godliness, through the knowledge of him who called us to his own glory and excellence, by which he has granted to us his precious and very great promises, so that through them you may become partakers of the divine nature, having escaped from the corruption that is in the world because of sinful desire (2 Peter 1:3-4, ESV).
This passage teaches that God, through His power, grants us what is necessary to be partakers of the divine nature. This habitual partaking is precisely what the Church means by sanctifying grace. (See CCC 1997-2000.)
The Church teaches that sanctifying grace is ordinarily received for the first time at Baptism; it is lost whenever one commits a grave (mortal) sin, and recovered ordinarily in the Sacrament of Reconciliation. (In any case, when the Sacrament of Reconciliation is impossibile for some reason, repentance for one’s sins motivated by supernatural love for God is sufficient—although a Catholic is still bound to receive the sacrament as soon as it is reasonably possible). (See CCC 1446 and 1999.)
With this association between sanctifying grace and Baptism in mind, it becomes easier to interpret passages such as
We were buried therefore with him by baptism into death, in order that, just as Christ was raised from the dead by the glory of the Father, we too might walk in newness of life (Romans 6:4).
or
having been buried with him in baptism, in which you were also raised with him through faith in the powerful working of God, who raised him from the dead (Col. 2:12).
or else
For as many of you as were baptized into Christ have put on Christ (Gal. 3:27).
What it is this “newness of life”? What have we “put on” with Baptism? None other than Christ himself. St. Paul thus confirms that Baptism produces an indwelling of God in our souls, one that produces a new kind of life in us. That is precisely what the Church means by sanctifying grace.
Similar reasoning permits St. Paul to say,
I have been crucified with Christ. It is no longer I who live, but Christ who lives in me. And the life I now live in the flesh I live by faith in the Son of God, who loved me and gave himself for me.
All of the supernatural actions that St. Paul is now capable of (faith, supernatural love), even while he is on earth (“in the flesh”), he does thanks to the presence of Christ in his soul.
That Baptism is the normal gateway to this indwelling of God is confirmed by many passages, but I will highlight the following from 1 Peter:
Baptism, which corresponds to this [i.e., the saving of Noah and his family in the Ark], now saves you, not as a removal of dirt from the body but as an appeal to God for a good conscience, through the resurrection of Jesus Christ (1 Peter 3:21).
Hence, Baptism, which produces a “good conscience” (i.e., forgiveness of sins), also saves us: that is, restores our friendship with God.
The necessity of sanctifying grace for salvation
Why is sanctifying grace absolutely necessary for salvation?
Heaven, first of all, consists in the direct or beatific vision of God, as St. Paul explains:
For now we see in a mirror dimly, but then face to face. Now I know in part; then I shall know fully, even as I have been fully known (1 Cor. 13:12).
But a mere creature simply does not have the capacity to bring himself to the Beatific Vision, under his own power. Rather, God must draw him to Himself:
No one can come to me unless the Father who sent me draws him. And I will raise him up on the last day (John 6:44).
Moreover, we men are born with a disadvantage; namely, sin and the tendency to sin. Most of us have committed actual offenses against God, and even those of us who have not are born deprived of the indwelling of God. Moreover, all of us easily tend to sinful behavior:
for all have sinned and fall short of the glory of God (Rom. 3:23).
Hence, we are in need of becoming truly righteous (i.e., of justification) and of becoming holy (sanctification). (See CCC 1987-1995.)
God accomplishes this by offering to dwell in our souls, as I noted; and this is precisely sanctifying grace. In other words, we cannot be right with God when deprived of union with Him—which is exactly the same as having sanctifying grace.
Is it just for God to demand the presence of sanctifying grace?
From what we have seen, since sanctifying grace is none other than the very indwelling of God in our souls, our participation in the divine nature (as St. Peter puts it), and going to Heaven consists in a perfect union with God (the Beatific Vision).
God brings us to the Beatific Vision—that is to heavenly glory—precisely by endowing us with a foretaste of that vision here on earth: namely, sanctifying grace.
It should come as no surprise, then, that being in the state of grace is the fundamental condition for going to Heaven.
Those who reject sanctifying grace (by committing a mortal sin), and persist in that rejection until death, are not in a position to enter the even closer union that people have with God in Heaven. They willfully rejected His presence in their soul while they were on earth; as a consequence, they will (unfortunately) not desire His presence, still less His direct Vision, after death.
Therefore, not only is it just for God to save only those who are in the state of grace, that is the only way He could save them. By endowing us with His presence “as in a mirror,” God draws us to Himself until we “see Him face-to-face.” But if we reject His presence, neither can God draw us to Himself.