What if we serve the poor and do many good deeds before we receive the gift of salvation, and then receive Christ in the hospital a day before death? Will God reward people for works done for the right reasons even though they didn’t yet know Christ?
There’s no direct biblical answer to that question, but 1 Corinthians 4:5 could be the key: “Therefore do not pronounce judgment before the time, before the Lord comes, who will bring to light the things now hidden in darkness and will disclose the purposes of the heart. Then each one will receive his commendation from God.” God knows the motives of people’s hearts.
For the believer, He knows when we’re trying to please Him by truly acting out of love for the needy, or when we’re doing good works for our own self-righteousness and the praise of others. He rewards good done with good motives, not good done with bad motives. If an unbeliever, before they came to faith, truly acts out of love more than self-congratulation and wanting the praise of others, then I wouldn’t be surprised if God rewarded them—as long as we remind ourselves that no good work in any way contributes to their salvation (Ephesians 2:8-9).
Acts 10 is also possibly pertinent, where the Holy Spirit comes upon Cornelius as the first Gentile Christ-follower. If he was not truly a genuine believer before this, it’s certainly obvious and remarkable that God was pleased with his good works before this, and again, I wouldn’t be surprised if God rewarded those good works:
At Caesarea there was a man named Cornelius, a centurion in what was known as the Italian Regiment. He and all his family were devout and God-fearing; he gave generously to those in need and prayed to God regularly. One day at about three in the afternoon he had a vision. He distinctly saw an angel of God, who came to him and said, “Cornelius!”
Cornelius stared at him in fear. “What is it, Lord?” he asked.
The angel answered, “Your prayers and gifts to the poor have come up as a memorial offering before God. Now send men to Joppa to bring back a man named Simon who is called Peter.He is staying with Simon the tanner, whose house is by the sea.”
…The men replied, “We have come from Cornelius the centurion. He is a righteous and God-fearing man, who is respected by all the Jewish people. A holy angel told him to ask you to come to his house so that he could hear what you have to say.” Then Peter invited the men into the house to be his guests. (Acts 10:1-6, 22-23).