Praise be to Allah.
Emission of wind invalidates wudoo', because of the report narrated by al-Bukhaari (135) from Abu Hurayrah (may Allah be pleased with him), who said: The Messenger of Allah (blessings and peace of Allah be upon him) said: “No prayer will be accepted from one who is in a state of minor impurity until he does wudoo’.” A man from Hadramawt said: What is minor impurity, O Abu Hurayrah? He said: Breaking wind silently or loudly.
Breaking wind is of two types:
Where there is a time when it stops, such as if it was coming out, then it stops for a while during which one can do wudoo' and pray. In this case, you have to do wudoo' and pray during the time when it stops.
Where it is continual and there is no time when it stops, rather it may come out at any time. In this case you should do wudoo' for every prayer after the time for that prayer begins, and pray the obligatory prayer and whatever naafil prayers you want with this wudoo’, and whatever wind comes out will not affect you, even if that is during prayer.
Shaykh al-Islam Ibn Taymiyah (may Allah have mercy on him) said:
If a person cannot remain in a state of purity for the length of the prayer, he should do wudoo' and pray, and it will not matter if anything comes out of him during the prayer, and this wudoo' will not be invalidated by that, according to the consensus of the imams. The most he has to do is do wudoo' for each prayer. End quote from Majmoo’ al-Fataawa, 21/221.
It says in Fataawa al-Lajnah al-Daa’imah (5/411):
The basic principle is that emission of wind invalidates wudoo', but if it comes out of a person constantly, he has to do wudoo' for each prayer when he wants to pray, then if it comes out of him whilst he is praying, it will not invalidate the prayer and he has to continue his prayer until he completes it, as a concession from Allah to His slaves and so as to relieve them of hardship, as Allah says (interpretation of the meaning):
“Allaah intends for you ease”
“and has not laid upon you in religion any hardship”
If what comes out has an unpleasant smell, it is not permissible for you to go to the mosque, because of the annoyance that this will cause to the worshippers and the angels.
Al-Bukhaari (5452) and Muslim (564) narrated from Jaabir ibn ‘Abd-Allaah that the Messenger of Allaah (blessings and peace of Allah be upon him) said: “Whoever eats garlic or onions, let him keep away from us, or keep away from our mosque and stay in his house.” According to a version narrated by Muslim: “Whoever eats onions or garlic or leeks, let him not come near our mosque, for the angels are offended by the same things that offend the sons of Adam.”
Muslim (567) narrated that ‘Umar (may Allah be pleased with him) said: O people, you eat two plants which I find to be nothing but repugnant, this onion and garlic. I remember the Messenger of Allaah (blessings and peace of Allah be upon him), if he noticed their smell coming from a man in the mosque, he would issue orders that he taken out to al-Baqee’. Whoever eats them, let him cook them to death.”
This indicates that it is obligatory to keep the mosque free of offensive odours and to prevent the one who has such an odour from coming to the mosque or staying in it.
It says in Kashshaaf al-Qinaa’ (2/365): It is Sunnah to protect every mosque from all kinds of dirt, filth, mucus, nail clippings and hair removed by clipping the moustache, shaving the head or plucking the armpits, because the mosques were not built for that. It is also Sunnah to protect it from offensive smells such as onion, garlic, leeks and similar things, even if there is no one in the mosque, because the Prophet (blessings and peace of Allah be upon him) said: “the angels are offended by the same things that offend the sons of Adam.” (Narrated by Ibn Maajah); and he said: “Whoever eats from these two plants, let him not come near our place of prayer.” and according to another report: “…let him not come near our mosques.” Narrated by al-Tirmidhi who said: It is hasan saheeh.
If a person who has eaten that – i.e., one who has an offensive smell of garlic and onions and the like – enters the mosque, it is strongly recommended to remove him, so as to remove the offensive thing.
By analogy, this also applies to one who passes wind from his backside in the mosque, because that annoys others with the smell. So it is Sunnah to protect the mosque from that and to make him leave for that reason. End quote.
But if the wind has no offensive smell, there is no reason why you should not enter the mosque and sit there. Imam al-Bukhaari (may Allah have mercy on him) said in his Saheeh: Chapter on breaking wudoo’ whilst in the Mosque.
Ibn Rajab (may Allah have mercy on him) said in Fath al-Baari:
What he (meaning al-Bukhaari) meant was that it is permissible to deliberately break one’s wudoo’ (pass wind) in the mosque because the Prophet (blessings and peace of Allah be upon him) mentioned it and did not forbid it, rather he stated that it interrupts the prayer of the angels.
Al-Hasan, ‘Ata’ and Ishaaq stated that there is a concession allowing deliberately breaking wudoo’ in the mosque.
It was stated above that it is permissible to sleep in the mosque in the case of necessity, and there is no difference of opinion on this point. That includes the sleep of the person who is observing i’tikaaf, because it is essential to the validity of his i’tikaaf, and it is also permissible in cases other than cases of necessity according to the majority, and it is most likely that one may pass wind whilst sleeping. So if it were not allowed to pass wind in the mosque, it would not be allowed to sleep in the mosque in any situation, but this is contrary to the texts and to scholarly consensus. End quote.
And Allah knows best.