Imam al-Nawawi's introduction to this chapter
أمَّا النِّيَاحَةُ فَحَرَامٌ، وَسَيَأتِي فِيهَا بَابٌ فِي كِتابِ النَّهْيِ، إنْ شَاءَ اللهُ تَعَالَى. وَأمَّا البُكَاءُ فَجَاءتْ أحَادِيثُ بِالنَّهْيِ عَنْهُ، وَأنَّ المَيِّتَ يُعَذَّبُ بِبُكَاءِ أهْلِهِ، وَهِيَ مُتَأَوَّلَةٌ ومَحْمُولَةٌ عَلَى مَنْ أوْصَى بِهِ، وَالنَّهْيُ إنَّمَا هُوَ عَن البُكَاءِ الَّذِي فِيهِ نَدْبٌ، أَوْ نِيَاحَةٌ، والدَّليلُ عَلَى جَوَازِ البُكَاءِ بِغَيْرِ نَدْبٍ وَلاَ نِياحَةٍ أحَادِيثُ كَثِيرَةٌ، مِنْهَا:
As for wailing (niyahah), it is forbidden, and a chapter on it will come, if Allah Almighty wills, in the Book of Prohibitions. As for weeping, some hadiths have come forbidding it and (stating) that the deceased is punished because of his family's weeping; these have been interpreted and taken to refer to someone who left instructions for (weeping over himself). The prohibition is only against weeping that involves lamenting (nadb) or wailing. The evidence that weeping without lamenting or wailing is permissible is many hadiths, among them (the following):
عن ابن عمر رضي الله عنهم: أنَّ رسول الله - صلى الله عليه وسلم - عاد سَعْدَ بْنَ عُبَادَةَ، وَمَعَهُ عَبدُ الرَّحْمانِ بْنُ عَوفٍ، وَسَعدُ بْنُ أَبي وَقَّاصٍ، وَعَبْدُ اللهِ بْنُ مَسْعُودٍ - رضي الله عنهما - فَبَكَى رسولُ الله - صلى الله عليه وسلم - فَلَمَّا رَأى القَوْمُ بُكَاءَ رسولِ الله - صلى الله عليه وسلم - بَكَوْا، فَقَالَ: «ألاَ تَسْمَعُونَ؟ إنَّ الله لاَ يُعَذِّبُ بِدَمْعِ العَينِ، وَلاَ بِحُزنِ القَلبِ، وَلَكِنْ يُعَذِّبُ بِهذَا أَوْ يَرْحَمُ» وَأشَارَ إِلَى لِسَانِهِ. متفقٌ عَلَيْهِ.
It is narrated from Sayyiduna Ibn Umar (may Allah be pleased with them both) that the Messenger of Allah (peace and blessings be upon him) visited (Sayyiduna) Sa'd bin Ubadah (may Allah be pleased with him) (in his illness), and with him were (Sayyiduna) Abd al-Rahman bin Awf, Sa'd bin Abi Waqqas and Abdullah bin Mas'ud (may Allah be pleased with them). The Messenger of Allah (peace and blessings be upon him) wept, and when the people saw the Messenger of Allah (peace and blessings be upon him) weeping, they wept too. He (peace and blessings be upon him) said: "Do you not hear? Indeed, Allah does not punish for the tears of the eye nor for the grief of the heart; rather, He punishes or shows mercy because of this," and he pointed to his tongue.
(Agreed upon)
وعن أُسَامَة بن زَيدٍ رضي الله عنهم: أنَّ رسول الله - صلى الله عليه وسلم - رُفِعَ إِلَيْهِ ابنُ ابْنَتِهِ وَهُوَ فِي المَوتِ، فَفَاضَتْ عَيْنَا رسولِ اللهِ - صلى الله عليه وسلم - فَقَالَ لَهُ سَعدٌ: مَا هَذَا يَا رسولَ الله؟! قَالَ: «هذِهِ رَحْمَةٌ جَعَلَهَا اللهُ تَعَالَى في قُلُوبِ عِبَادِهِ، وَإنَّمَا يَرْحَمُ اللهُ مِنْ عِبَادِهِ الرُّحَمَاءَ». متفقٌ عَلَيْهِ.
It is narrated from Sayyiduna Usamah bin Zayd (may Allah be pleased with them both) that the son of his daughter (his grandson) was brought to the Messenger of Allah (peace and blessings be upon him) while he was in the throes of death, and the eyes of the Messenger of Allah (peace and blessings be upon him) overflowed with tears. Sayyiduna Sa'd (may Allah be pleased with him) said to him: "O Messenger of Allah, what is this?!" He (peace and blessings be upon him) said: "This is mercy that Allah Almighty has placed in the hearts of His servants, and Allah shows mercy only to those of His servants who are merciful."
(Agreed upon)
وعن أنسٍ - رضي الله عنه: أنَّ رسول الله - صلى الله عليه وسلم - دَخَلَ عَلَى ابْنِهِ إبْرَاهيمَ - رضي الله عنه - وَهُوَ يَجُودُ بِنَفسِهِ، فَجَعَلَتْ عَيْنَا رسولِ الله - صلى الله عليه وسلم - تَذْرِفَان. فَقَالَ لَهُ عبدُ الرحمانِ بن عَوف: وأنت يَا رسولَ الله؟! فَقَالَ: «يَا ابْنَ عَوْفٍ إنَّهَا رَحْمَةٌ» ثُمَّ أتْبَعَهَا بأُخْرَى، فَقَالَ: «إنَّ العَيْنَ تَدْمَعُ والقَلب يَحْزنُ، وَلاَ نَقُولُ إِلاَّ مَا يُرْضِي رَبَّنَا، وَإنَّا لِفِرَاقِكَ يَا إبرَاهِيمُ لَمَحزُونُونَ». رواه البخاري، وروى مسلم بعضه. والأحاديث في الباب كثيرة في الصحيح مشهورة، والله أعلم.
It is narrated from Sayyiduna Anas (may Allah be pleased with him) that the Messenger of Allah (peace and blessings be upon him) went in to his son Ibrahim (may Allah be pleased with him) while he was in the throes of death, and the eyes of the Messenger of Allah (peace and blessings be upon him) filled with tears. Sayyiduna Abdur Rahman bin Awf (may Allah be pleased with him) said to him: "And you too (weep), O Messenger of Allah?!" He (peace and blessings be upon him) said: "O Ibn Awf, it is mercy." Then he followed it with another statement and said: "Indeed the eye sheds tears and the heart grieves, and we say only what pleases our Lord, and O Ibrahim, we are truly grieved at parting from you."
(Narrated by al-Bukhari, and Muslim narrated part of it)
The hadiths on this chapter are many and well known in the Sahih (collections). And Allah knows best.