This excerpt is taken from Sheikh Abu Hamza Abdul Khaliq Siddiqi's book Sahih Fazail-e-Amaal.
Allah Almighty says:
﴿وَلَا تُصَلِّ عَلَىٰ أَحَدٍ مِّنْهُم مَّاتَ أَبَدًا وَلَا تَقُمْ عَلَىٰ قَبْرِهِ ۖ إِنَّهُمْ كَفَرُوا بِاللَّهِ وَرَسُولِهِ وَمَاتُوا وَهُمْ فَاسِقُونَ﴾ .
"And whoever among them dies, do not pray the funeral prayer for him, nor stand by his grave; they have disbelieved in Allah and His Messenger, and their death will be in a state of disbelief."
Reference: (9-At-Tawbah:84)
From this noble verse, it is understood that it is not correct to perform the funeral prayer for disbelievers, polytheists, and hypocrites, because these people are rebels against Islam and enemies of Allah, and it is not permissible to pray for such people.
Inevitably, when it is not correct to perform the funeral prayer for these people, it is understood that the funeral prayer will be performed only for Muslims.
عن ابن عباس رضي الله عنهما قال: سمعت رسول الله صلى الله عليه وسلم يقول: ما من رجل مسلم يموت فيقوم على جنازته أربعون رجلا ، لا يشركون بالله شيئا إلا شفعهم الله فيه
It is narrated from Sayyiduna Ibn Abbas (may Allah be pleased with them) that he heard the Messenger of Allah (peace be upon him) say: "If a Muslim person dies and forty people who do not associate anyone with Allah perform his funeral prayer, then Allah will accept their intercession on behalf of the deceased."
Reference: Sahih Muslim, Book of Funerals, Chapter: Whoever prays over him forty intercedes for him, No: 948.
عن أبى هريرة رضى الله عنه أن رسول الله صلى الله عليه وسلم قال: من اتبع جنازة مسلم إيمانا واحتسابا، وكان معه حتى يصلي عليها ويفرغ من دفنها ، فإنه يرجع من الأجر بقيراطين كل قيراط مثل أحد ، ومن صلى عليها ثم رجع قبل أن تدفن فإنه يرجع بقيراط
The Messenger of Allah (peace be upon him) said: "Whoever follows the funeral of a Muslim with the intention of faith and reward, and remains with it until the prayer is performed and the burial is completed, will return with two qirats of reward, each qirat is like a mountain. And whoever only prays the funeral prayer and then returns before the burial will return with one qirat."
Reference: Sahih Bukhari, Book of Faith, Chapter: Following the Funeral is part of Faith, No: 47.
عن أبى هريرة رضى الله عنه قال: قال رسول الله صلى الله عليه وسلم : من شهد الجنازة حتى يصلى عليها فله قيراط ، ومن شهدها حتى تدفن فله قيراطان . قيل: وما القيراطان؟ قال: مثل الجبلين العظيمين
It is narrated from Sayyiduna Abu Hurairah (may Allah be pleased with him) that the Messenger of Allah (peace and blessings be upon him) said: "Whoever attends the funeral until the prayer is performed for it will have a reward of one qirat. And whoever stays until the burial will have a reward of two qirats." It was asked, "What is the amount of two qirats?" He (peace be upon him) said: "Like two large mountains."
Reference: Sahih Bukhari, Book of Funerals, Chapter: Whoever waits until burial, No: 1325. Sahih Muslim, Book of Funerals, Chapter: The virtue of praying over the funeral and following it, No: 945.
The Noble Prophet (peace be upon him) mentioned further mercy of Allah, and consider how Allah has made various excuses for His servants to forgive:
"Whoever washes the deceased and shrouds him, Allah will forgive him forty times; and whoever shrouds the deceased, Allah will clothe him in brocade and silk garments in Paradise; and whoever digs the grave for the deceased and buries him in it, Allah will grant him as much reward as the right of a person who allocates a residence for someone until the Day of Resurrection."
Reference: At-Targhib wa At-Tarhib, No: 3492 – Mustadrak Hakim: 1/362,354.