If an unbaptized person dies while preparing for his baptism, or is martyred for his faith in Jesus, then he goes right to Heaven and the stance of the Church is that his blood baptized him.
If an adult dies without being baptized and he didn’t get a chance to embrace Christ, then if he lived righteously he’ll still go to Heaven because he would have been baptized if he desired it.
If a child dies without being baptized, the best we can do is hope that God is merciful. Having said that the Catechism notes specifically that Jesus loved children, and all of the most recent Popes have publicly stated that they believe unbaptized children go to Heaven. Personally, given everything I know about Catholicism, I think it would be out of character for God to keep unbaptized children out of Heaven.
They may not have personally known Jesus, but they did expect a Messiah would come. There were many prophecies about His Coming.
There are clues in the Gospels about their knowing of His Coming despite their not physically being on earth anymore…:
From BibleHub, Matthew 27 NIV:
50And when Jesus had cried out again in a loud voice, he gave up his spirit. 51At that moment the curtain of the temple was torn in two from top to bottom. The earth shook, the rocks split 52and the tombs broke open. The bodies of many holy people who had died were raised to life. 53They came out of the tombs after Jesus’ resurrection andewent into the holy city and appeared to many people. 54When the centurion and those with him who were guarding Jesus saw the earthquake and all that had happened, they were terrified, and exclaimed, “Surely he was the Son of God!”
From BibleHub, John 8 NIV:
51Very truly I tell you, whoever obeys my word will never see death.” 52At this they exclaimed, “Now we know that you are demon-possessed! Abraham died and so did the prophets, yet you say that whoever obeys your word will never taste death. 53Are you greater than our father Abraham? He died, and so did the prophets. Who do you think you are?” 54Jesus replied, “If I glorify myself, my glory means nothing. My Father, whom you claim as your God, is the one who glorifies me. 55Though you do not know him, I know him. If I said I did not, I would be a liar like you, but I do know him and obey his word. 56Your father Abraham rejoiced at the thought of seeing my day; he saw it and was glad.” 57“You are not yet fifty years old,” they said to him, “and you have seen Abraham!” 58“Very truly I tell you,” Jesus answered, “before Abraham was born, I am!” 59At this, they picked up stones to stone him, but Jesus hid himself, slipping away from the temple grounds.
18Then the Sadducees, who say there is no resurrection, came to him with a question. 19“Teacher,” they said, “Moses wrote for us that if a man’s brother dies and leaves a wife but no children, the man must marry the widow and raise up offspring for his brother. 20Now there were seven brothers. The first one married and died without leaving any children. 21The second one married the widow, but he also died, leaving no child. It was the same with the third. 22In fact, none of the seven left any children. Last of all, the woman died too. 23At the resurrection c whose wife will she be, since the seven were married to her?” 24Jesus replied, “Are you not in error because you do not know the Scriptures or the power of God? 25When the dead rise, they will neither marry nor be given in marriage; they will be like the angels in heaven. 26Now about the dead rising—have you not read in the Book of Moses, in the account of the burning bush, how God said to him, ‘I am the God of Abraham, the God of Isaac, and the God of Jacob’d ? 27He is not the God of the dead, but of the living. You are badly mistaken!”
27Some of the Sadducees, who say there is no resurrection, came to Jesus with a question. 28“Teacher,” they said, “Moses wrote for us that if a man’s brother dies and leaves a wife but no children, the man must marry the widow and raise up offspring for his brother. 29Now there were seven brothers. The first one married a woman and died childless. 30The second 31and then the third married her, and in the same way the seven died, leaving no children. 32Finally, the woman died too. 33Now then, at the resurrection whose wife will she be, since the seven were married to her?” 34Jesus replied, “The people of this age marry and are given in marriage. 35But those who are considered worthy of taking part in the age to come and in the resurrection from the dead will neither marry nor be given in marriage, 36and they can no longer die; for they are like the angels. They are God’s children, since they are children of the resurrection. 37But in the account of the burning bush, even Moses showed that the dead rise, for he calls the Lord ‘the God of Abraham, and the God of Isaac, and the God of Jacob.’b 38He is not the God of the dead, but of the living, for to him all are alive.” 39Some of the teachers of the law responded, “Well said, teacher!” 40And no one dared to ask him any more questions.
From BibleHub, John 11 NIV:
23Jesus said to her, “Your brother will rise again.” 24Martha answered, “I know he will rise again in the resurrection at the last day.” 25Jesus said to her, “I am the resurrection and the life. The one who believes in me will live, even though they die; 26and whoever lives by believing in me will never die. Do you believe this?” 27“Yes, Lord,” she replied, “I believe that you are the Messiah, the Son of God, who is to come into the world.”
I wanted to add that the Transfiguration also placed those who were long gone in Jesus Christ’s Presence:
2 Peter 1:16–18
1 Peter 3 BibleHub:
18For Christ also suffered once for sins, the righteous for the unrighteous, to bring you to God. He was put to death in the body but made alive in the Spirit. 19After being made alive, d he went and made proclamation to the imprisoned spirits— 20to those who were disobedient long ago when God waited patiently in the days of Noah while the ark was being built. In it only a few people, eight in all, were saved through water, 21and this water symbolizes baptism that now saves you also—not the removal of dirt from the body but the pledge of a clear conscience toward God. e It saves you by the resurrection of Jesus Christ, 22who has gone into heaven and is at God’s right hand—with angels, authorities and powers in submission to him.
1 Peter 4 BibleHub: 6For this is the reason the gospel was preached even to those who are now dead, so that they might be judged according to human standards in regard to the body, but live according to God in regard to the spirit.
1260 "Since Christ died for all, and since all men are in fact called to one and the same destiny, which is divine, we must hold that the Holy Spirit offers to all the possibility of being made partakers, in a way known to God, of the Paschal mystery."62 Every man who is ignorant of the Gospel of Christ and of his Church, but seeks the truth and does the will of God in accordance with his understanding of it, can be saved. It may be supposed that such persons would have desired Baptism explicitly if they had known its necessity.
1261 As regards children who have died without Baptism, the Church can only entrust them to the mercy of God …
Well… We can do more than hope God is merciful. God is abundantly merciful. However, the evidence that Adam’s original sin has taken effect, is in the act of sin itself. What act of sin is a two month old baby guilty of? It would be obserd to believe that God’s mercy will not cover a child.
And, according to Judaism, whether or not we “make it” to heaven should NOT be our focus here on earth. Loving Gd and our fellow humans, caring for animals, and making the world a better place for all: this should be our preoccupation.