Can you repent from your sins right before you die and still go to heaven?
Perhaps in theory, but it would be difficult to accomplish since we generally do not know when we will die. "Come now, you who say, "Today or tomorrow we will go to such and such a city, spend a year there, buy and sell, and make a profit"; whereas you do not know what will happen tomorrow. For what is your life? It is even a vapor that appears for a little time and then vanishes away" (James 4:13-14). People tend to procrastinate, and it is so easy to put off serving God too long. That is why the Solomon gave this advise: "Remember now your Creator in the days of your youth, before the difficult days come, and the years draw near when you say, "I have no pleasure in them"" (Ecclesiastes 12:1).
Then there is the question of honesty. If a person purposely puts off serving God, but seeing that life is going to end shortly, decides to attempt to stack the odds in his favor, how do you think God will judge his "repentance?" You see, God doesn't just judge what we do, he also examines our motives. "Therefore judge nothing before the time, until the Lord comes, who will both bring to light the hidden things of darkness and reveal the counsels of the hearts. Then each one's praise will come from God" (I Corinthians 4:5).
But to balance this, there are people who honestly turn to the Lord, repenting of theirs sins, were baptized for the remission of their sins, and had just begun to walk the new life when their life ended. Such situations is the point of the parable of the vineyard workers.
"For the kingdom of heaven is like a landowner who went out early in the morning to hire laborers for his vineyard. Now when he had agreed with the laborers for a denarius a day, he sent them into his vineyard. And he went out about the third hour and saw others standing idle in the marketplace, and said to them, 'You also go into the vineyard, and whatever is right I will give you.' So they went. Again he went out about the sixth and the ninth hour, and did likewise. And about the eleventh hour he went out and found others standing idle, and said to them, 'Why have you been standing here idle all day?' They said to him, 'Because no one hired us.' He said to them, 'You also go into the vineyard, and whatever is right you will receive.' So when evening had come, the owner of the vineyard said to his steward, 'Call the laborers and give them their wages, beginning with the last to the first.' And when those came who were hired about the eleventh hour, they each received a denarius. But when the first came, they supposed that they would receive more; and they likewise received each a denarius. And when they had received it, they complained against the landowner, saying, 'These last men have worked only one hour, and you made them equal to us who have borne the burden and the heat of the day.' But he answered one of them and said, 'Friend, I am doing you no wrong. Did you not agree with me for a denarius? Take what is yours and go your way. I wish to give to this last man the same as to you. Is it not lawful for me to do what I wish with my own things? Or is your eye evil because I am good?' So the last will be first, and the first last. For many are called, but few chosen" (Matthew 20:1-16).
The point is that God doesn't examine who you once were. He looks at who you are. "But if a wicked man turns from all his sins which he has committed, keeps all My statutes, and does what is lawful and right, he shall surely live; he shall not die. None of the transgressions which he has committed shall be remembered against him; because of the righteousness which he has done, he shall live. Do I have any pleasure at all that the wicked should die?" says the Lord GOD, "and not that he should turn from his ways and live? But when a righteous man turns away from his righteousness and commits iniquity, and does according to all the abominations that the wicked man does, shall he live? All the righteousness which he has done shall not be remembered; because of the unfaithfulness of which he is guilty and the sin which he has committed, because of them he shall die" (Ezekiel 18:21-24).