Assalamualaikum w.b.t. some people pray while wearing their pyjamas without first changing their clothes. So, I want to ask, what is the ruling of praying while wearing pyjamas?
Alhamdulillah, praise and gratitude to Allah SWT for His countless blessings for us all. Praise and salutations to our beloved Prophet Muhammad PBUH, his family, companions and all those who follow his footsteps until the Last Day.
Among the valid conditions of prayer is that one’s clothing, place of prayer and body must be clean and free from najis. This follows the statement of Allah SWT:
“And your clothing purify,” 
Syeikh Dr Wahbah al-Zuhaili in his commentary stated that this verse means to make sure that one’s clothing must be free from najis and filth. 
Likewise, in aa hadith narrated from Abu Hurairah RA, he said:
أَنَّ خَوْلَةَ بِنْتَ يَسَارٍ أَتَتِ النَّبِيَّ صلى الله عليه وسلم فَقَالَتْ : يَا رَسُولَ اللهِ إِنَّهُ لَيْسَ لِي إِلاَّ ثَوْبٌ وَاحِدٌ وَأَنَا أَحِيضُ فِيهِ فَكَيْفَ أَصْنَعُ ؟ قَالَ : إِذَا طَهُرْتِ فَاغْسِلِيهِ ، ثُمَّ صَلِّي فِيهِ . فَقَالَتْ : فَإِنْ لَمْ يَخْرُجِ الدَّمُ ؟ قَالَ : يَكْفِيكِ غَسْلُ الدَّمِ وَلاَ يَضُرُّكِ أَثَرُه
“Khawlah daughter of Yasar came to the Prophet (ﷺ) and said: Messenger of Allah, I have only one clothe and I menstruate in it, how should I do ? He said: When you are purified, wash it and pray in it. She asked: If the blood is not removed, (then what) ? He said: It is enough for you to wash the blood, its mark will not do any harm to you.” 
This shows that if blood comes in contact with one’s clothes, then it is obligatory for the najis to be washed and removed from it. If it is not najis, surely the Prophet PBUH wouldn’t command as such.
Hence, it is included in the valid conditions of prayer that one’s body, clothes and place of prayer must be pure and free from any najis. Al-Khatib al-Syarbini said: “The fifth condition is that the clothes and body must be free from unforgiven najis, that is one’s clothes and body even if it is inside his nose, mouth, eyes or ears. (Another matter that must be pure) is the place of prayer. A prayer is invalid if there are najis in any of the above situations, even when the person is ignorant or doesn’t know of the presence of the najis, it invalidates the prayer.” 
Hence, if there is unforgiven najis on one’s clothes or place of prayer, then his prayer is invalid. Likewise, if he bears najis on his clothes that came into contact with najis, then his prayer is invalid.
Imam al-Syirazi said: “If a person wears clean clothing and at its end comes into contact with najis, for instance, a person wears a turban and its ends fall on the soil that has najis, then his prayer is invalid because he bears what is connected with the najis.”
Whereas, Imam al-Nawawi in his commentary stated: “What is mentioned by the author is something that has been agreed. It is the same as if the ends came into contact with najis when one is standing, sitting, performing rukuk and sujud or when one isn’t moving. This is our madhhab and there isn’t khilaf in it.” 
Thus, the prayer of a person is invalid if there are najis on his body or the place where he prayed or if there are najis on himself.
The issue of praying while wearing pyjamas
Basically, as explained above, the valid condition of prayer is to ensure that one’s body, place of prayer and clothing is pure and clean. Especially when the clothes are the ones going to be worn in the prayer must be free and clean from any najis. This is based on the statement of Allah SWT:
“And your clothing purify,” 
Syeikh ‘Abd al-Rahman al-Sa’di in his commentary stated that what is meant in this verse is that the clothing must be cleaned from najis. The reason is it is one of the perfections specific for prayer. Hence, it is commanded to wash the cloth and cleaned it from any type of najis all the time especially when one is praying. 
Likewise, it can also be understood that any najis must be cleaned through the commandment of Prophet PBUH to Khaulah binti Yasar. Khaulah said to Rasullullah PBUH:
يَا رَسُولَ اللهِ إِنَّهُ لَيْسَ لِي إِلاَّ ثَوْبٌ وَاحِدٌ وَأَنَا أَحِيضُ فِيهِ فَكَيْفَ أَصْنَعُ ؟ قَالَ : إِذَا طَهُرْتِ فَاغْسِلِيهِ ، ثُمَّ صَلِّي فِيهِ
“Messenger of Allah, I have only one clothing and I menstruate in it, how should I do? He said: When you are purified, wash it and pray in it.” 
This shows that the Prophet PBUH commanded for the clothing being worn in prayer to be cleansed beforehand from any najis.
Imam al-Nawawi said, according to madhhab Syafi’I, being clean and free from najis is a valid condition of prayer. If he knows that there is najis, then his prayer is invalid without any khilaf. If he forgot or is ignorant that there is najis on his body, clothing and place of prayer, then his prayer is invalid too. Regardless of whether it is the obligatory, sunnah or funeral prayer or even the tilawah prostration. Removing najis is a valid condition for all these worships. This is the opinion of the Syafi’iyyah scholars, Imam Ahmad and the jumhur of the scholars. Regardless of whether they are the salaf or khalaf scholars. 
Syeikh al-Jawi said: “The second valid condition is to be clean from unforgiven najis (لا يعفى عنها) on one’s clothing, one being clean from najis includes the nose, eyes and ears. The place of prayer must also be clean from najis for the najis may come into contact on one’s body or clothes.” 
This means that a person’s prayer is valid or invalid depending on this condition. If it isn’t fulfilled or followed then the prayer is invalid.
Among the other condition of prayer is to completely cover the awrah. This is based on the rebuke of the Prophet PBUH to Jarhad when his thighs were exposed in prayer. The Prophet PBUH said:
غَطِّ فَخِذَكَ فَإِنَّهَا مِنْ الْعَوْرَةِ
“Cover your thigh, for indeed it is ‘Awrah.” 
In another narration it is stated from Aisyah R.Anha, the Prophet PBUH said:
لاَ يَقْبَلُ اللَّهُ صَلاَةَ حَائِضٍ إِلاَّ بِخِمَارٍ
“The Salat (prayer) of a woman, who has reached puberty, is not accepted unless she is wearing a Khimar.” 
Imam al-San’ani stated that the denial with the letter lam in the hadith is a denial with the meaning that the woman’s prayer is invalid if she doesn’t cover her awrah. 
Imam al-Nawawi in al-Majmu’ said: “Covering the awrah is a valid condition of prayer. If any part of awrah of a person is exposed in prayer, then the prayer is invalid. Regardless of whether the exposed part is a lot or just a little, for both man and woman. Whether the prayer is performed in congregation or alone/ Whether it is an obligatory, sunnah or funeral prayer or tawaf or tilawah prostration or the prostration of gratitude.” 
Coming back to the presented question, it is permissible to wear pyjamas to pray as long as the clothing is clean and pure from any najis as well as covers the awrah. This follows the Islamic legal maxim which states:
الأَصْلُ فِي الأشْيَاء الطَّهَارةُ
“The origin of something is its purity.”
This means any clothing, water and others remain pure as long as no evidence proves otherwise. Syeikh ‘Abd al-Rahman al-Sa’di said:
والأصل في مياهنا الطهارهْ # والأرض والثياب والحجارهْ
“The origin of water is clean and pure. Likewise for soil, clothes and rocks.” 
However, it is best to change one’s clothes into proper and appropriate attire. This is in accordance with the statement of Allah SWT:
يَـٰبَنِىٓ ءَادَمَ خُذُوا۟ زِينَتَكُمْ عِندَ كُلِّ مَسْجِدٍ
“O children of Adam, take your adornment at every masjid,” 
Ibn Kathir stated that in this verse, there is a meaning that it is sunnah to wear proper and suitable attire when praying. It is also encouraged to wear perfumes and brush one’s teeth, for they are included in the meaning of adornments and a part of the perfection of worship. 
According to the above question, we state that it is permissible to pray wearing one’s pyjamas as long as the clothing is clean and pure as well as covers one’s awrah. However, it is best to change into a better and proper attire, neat and suitable. This is a preparation to face the One and Only God.
 Surah al-Muddathir: 4
 See al-Tafsir al-Munir, 29/210
 Narrated by Abu Daud (365)
 See Mughni al-Muhtaj, 1/402
 See al-Majmu’ Syarh al-Muhazzab, 2/150
 Surah al-Muddatthir : 4
 See Taisir al-Karim al-Rahman fi Tafsir al-Kalam al-Mannan, 2/132
 Narrated by Abu Daud (365)
 See al-Majmu’ Syarh al-Muhazzab, 3/139
 See Kasyifah al-Saja Syarh Safinatunnaja, pg. 119
 Narrated by al-Tirmizi (2798) He stated this hadith is hasan.
 Narrated by Abu Daud (641)
 See Subul al-Salam Ila Bulugh al-Maram, 14/43
 See al-Majmu’ Syarh al-Muhazzab, 3/172
 See Syarh Manzumah al-Qawaid al-Fiqhiyah, pg. 136-137
 Surah al-‘Araf: 31
 See Tafsir al-Quran al-‘Azim, 2/256