This is a great question, and one that Christians have been talking about since the beginning of the Church. St. Paul tells us that we are justified by faith, and Christ himself told us that unless we are born again of water and the Spirit, one cannot enter the kingdom of God (John 3:5). Here Jesus refers to the sacrament of baptism. But, at the same time, Jesus told the good thief on the cross, “Today, you will be with me in paradise.” The good thief was probably never baptized, but he did express explicit faith in Jesus. This example proves that even if someone has not received the sacrament of baptism, God is able to give sanctifying and saving grace by another means. In the early Church, there were many martyrs who died as Catechumens, meaning they were still in the preparation phase and had not yet been baptized. The Church has said that these martyrs received a “baptism of blood.” They were not literally baptized, but received salvation by their faith and their witness to Christ. Other people, such as the good thief already mentioned, have received a “baptism of desire” in which they explicitly expressed faith, but for some reason, had not actually been baptized.
A related question is what happened to the Jews who lived before Jesus. After all, they never received the sacrament of baptism, either! I think St. Joseph is a good example to consider. The Scriptures calls him a “just man,” and considering that he was entrusted with caring for God himself, he must have been very holy. However, it is likely that he died before Jesus instituted the sacrament of baptism. Sometimes Church authors refer to the “sacraments” of the Old Covenant. This refers to the rituals and symbols that God gave the Israelites in the various covenants of the Old Testament. For Jews, a major symbol of the Covenant was circumcision, and it has been understood that God also gave sanctifying grace through these “sacraments.” They are not in the same category as the seven sacraments which Christ gave the Church, but they were still occasions on which God gave grace to his people. The sacrifices were only a prefigurement of the ultimate sacrifice of Jesus on the Cross. God gave grace in the Old Testament, because he foresaw the merits of Christ’s death on the cross. The Jews were (and can be today) justified by faith, through the Covenants that God has given their people. So, St. Joseph is certainly in heaven, as are all of our Fathers in the faith such as Abraham, Moses, and David.
When faithful Jews died, the gates of heaven were not yet open, so they went to some sort of spiritual “waiting area” until the Savior would open the gates of heaven again. The event of Jesus coming to retrieve these just ones is sometimes called the “harrowing of hell,” referring to the line in the Creed when we say that Jesus “descended into hell.” This is not the same hell that we typically think of today, which is a state of eternal separation from God. Rather, it was the place the dead went while heaven was not accessible.
A more challenging question to consider is about those who have never known about Jesus, or have known about the Gospel, but not in a compelling way. Plus, what about babies who died without the benefit of baptism? In short, the Church hopes and prays for the salvation of all people, even those who do not explicitly know Christ. They too can be saved by a sort of “baptism of desire,” although only in an implicit way, because they do not know that baptism and faith are necessary for salvation. Some people are seeking to live the truth in their lives, but have not yet seen that Jesus is the Truth. God can give graces in ways that we are not aware of. Other religious traditions such as indigenous religions, Buddhism, and Hinduism are not completely devoid of truth or goodness. They can be understood as a preparation to receive the fullness of religious truth through Jesus Christ. God can certainly use these other religious practices as occasions to give saving grace.
A traditional way of understanding the Church’s role in salvation is in this Latin phrase: “Extra ecclesiam nulla salus.” This translates as, “Outside the Church, there is no salvation.” A few have incorrectly interpreted this to mean that only baptized Catholics can be saved. This is obviously not true, based on the story of the good thief. This phrase means that whenever God gives grace to a person, it always comes through the Church, the Body of Christ. Ideally, this is through the seven sacraments. But if God were to give grace to a truth-seeking atheist, this grace also comes through Christ and the Church. In every Mass, the Church prays for the salvation of the whole world. I like Eucharist Prayer IV, which includes this prayer to God the Father: “Lord, remember now all for whom we offer this sacrifice: Francis our Pope, … your entire people, and all who seek you with a sincere heart. Remember also those … whose faith you alone have known.”
The Catechism says in #994, “It is Jesus himself who on the last day will raise up those who have believed in him, who have eaten his body and drunk his blood.” We can understand this to mean that even people who implicitly believed in him can also be raised up on the last day. Of course, I do not mean to diminish in any way the value of the sacraments that Jesus gave us. He especially gave us Baptism as a systematic way to receive his sanctifying grace and forgiveness of our sins. In a world as big and messy as it is, the structured sacramental economy that we have can give us reassurance that we indeed are in the state of grace, and that we have concrete encounters with Christ through the Church.