If someone remembers that he is junub after he has prayed, he must do ghusl and repeat the prayer that he prayed when he was junub, as soon as he remembers that.
Allah, may He be Exalted, has explained that purification from janabah is to be done by means of ghusl. The one who has no access to water, or cannot use it because of sickness and the like, must do tayammum, because wudu does not remove janabah.
After mentioning wudu for prayer, Allah explains that the ruling on janabah is different from wudu; what is required is ghusl. He, may He be Exalted, says:
O you who believe, when you prepare for prayer, wash your faces, and your hands [and arms] to the elbows; wipe over your heads [with wet hands]; and [wash] your feet to the ankles. If you are in a state of major impurity [janabah], then purify yourselves [ghusl]. If you are ill, or on a journey, or have relieved yourselves, or you have touched women, and cannot find water, then find some clean earth, and wipe your faces and hands with it [tayammum]} [Al-Ma’idah 5:6]
{O you who believe, do not approach the prayer when you are intoxicated, until you know what you are saying, nor in a state of major impurity [janabah] – except while passing through – until you have washed yourselves completely [ghusl]. If you are ill, or on a journey, or have relieved yourselves, or you have touched women, and cannot find water, then find some clean earth, and wipe your faces and hands with it [tayammum]. For Allah is Pardoning, Oft-Forgiving} [An-Nisa’ 4:43].
II.
What you should have done when you remembered that you had prayed when junub, was gto o and do ghusl in your house or anywhere else, because you are in a city where water is available, and you could have gone back home to do ghusl, or gone and done ghusl in any public bath house. And you have to repeat that prayer.
It was narrated that Abu Hurayrah (may Allah be pleased with him) said: The iqamah for prayer was given, and we stood up and made the rows straight before the Messenger of Allah (blessings and peace of Allah be upon him) came out to us. Then the Messenger of Allah (blessings and peace of Allah be upon him) came and stood in his prayer place before saying the takbir, then he remembered something, so he turned to leave and said to us: “Stay where you are.” We remained standing, waiting for him, until he came out to us. He had done ghusl, and his head was dripping water. Then he said takbir and led us in prayer. Narrated by Muslim, 605.
But if it so happened that you could not do ghusl at all, then you should have done tayammum to purify yourself from janabah, then done wudu for prayer, and continued to pray on that basis until you could find water and were able to use it.
The Prophet (blessings and peace of Allah be upon him) instructed the Sahabi who was junub, when they had no water with them, to do tayammum, Then when water became available, he instructed him to do ghusl.
In Sahih al-Bukhari, in the lengthy hadith of `Imran ibn Husayn about the story of the man who stayed away and did not pray with the Prophet (blessings and peace of Allah be upon him), it says that the Prophet (blessings and peace of Allah be upon him) asked him: “What kept you from praying with us?” He said: I became junub and there is no water. He said: “You should use clean earth [and do tayammum], and that will suffice you.” Then water became available, and he gave to the one who had become junub a vessel of water, and he said: “Go and pour it over yourself” – meaning do ghusl with it. Narrated by al-Bukhari.
It is not permissible to pray with wudu when you are junub, and wudu does not suffice instead of ghusl.
For more information, see the answer to question no. 305664.
Shaykh Ibn `Uthaymin (may Allah have mercy on him) was asked: One day I woke up and I was junub, but I did not do ghusl, and I prayed Fajr and Zuhr when I was junub. Will my prayers be accepted, or do I have to repent and repeat those two prayers?
He (may Allah have mercy on him) replied: If you knew that you were junub and you prayed when you were aware of that, then you are in grave danger, because some of the scholars say that whoever prays knowing that he is in a state of impurity is a disbeliever - we ask Allah to keep us safe and sound – because he is like one who mocks the verses of Allah.
But if you were unaware and you did not realise that you were junub until after you had prayed Fajr and Zuhr, then you do not have to do anything except do ghusl and repeat those two prayers. Similarly, if you had realised that you were junub but then forgot, so you prayed Fajr and Zuhr, you do not have to do anything except do ghusl and repeat those two prayers.
End quote from Fatawa Nur `ala ad-Darb by al-Uthaymin, 8/2).
And Allah knows best.