Praise be to Allah.
The basic principle is that a naafil Hajj is better than giving the money that would be used for this Hajj in charity. But there may be cases where giving the money in charity is better than doing a naafil Hajj, such as if the money is spent on jihad for the sake of Allaah, or for calling people to Allaah (da’wah), or on needy people, especially if they are relatives.
Shaykh al-Islam Ibn Taymiyah said in al-Ikhtiyaaraat (p. 206)
Hajj in the prescribed manner is better than charity that is not obligatory. But if a person has needy relatives, then giving charity to them is better. The same applies if there are people who need his help. But if both of them are voluntary, then Hajj is better because it is an act of worship which is both physical and financial. By the same token, udhiyah (sacrifice on Eid al-Adha) and ‘aqeeqah (sacrifice offered when a baby is born) is better than giving the price of the animal in charity. But that is subject to the condition that the person does religious duties on the way, abstains from haraam things, offers all five daily prayers, speaks the truth, fulfils trusts and does not violate the rights of anyone.
Shaykh Ibn Baaz (may Allaah have mercy on him) said:
Hajj and ‘Umrah are better than giving the equivalent amount of money in charity, for the one whose intention is sincerely for the sake of Allaah alone and does these rituals in the manner prescribed in sharee’ah. It was narrated in a saheeh report that the Prophet (peace and blessings of Allaah be upon him) said: ‘From one ‘Umrah to the next is an expiation for whatever came in between, and an accepted Hajj brings no less a reward than Paradise.’ Narrated by al-Bukhaari, 1773; Muslim, 1349. And he (peace and blessings of Allaah be upon him) said: ‘ ‘Umrah in Ramadaan is equivalent to Hajj.’ Narrated by al-Bukhaari, 1782; Muslim, 1256.”
And he said:
“Whoever has done the obligatory Hajj, it is better for him to donate the expense of his second Hajj to the mujaahideen who are striving for the sake of Allaah, because when the Prophet (peace and blessings of Allaah be upon him) was asked which deed is best, he said, ‘Belief in Allaah and His Messenger.’ He was asked, ‘Then what?’ He said, ‘Jihad for the sake of Allaah.’ He was asked, ‘Then what?’ He said, ‘An accepted Hajj.’ Narrated by al-Bukhaari, 26; Muslim, 83.
So Hajj comes after jihad. What is meant here is the naafil Hajj, because the obligatory Hajj is one of the pillars of Islam, so long as one is able to do it. In al-Saheehayn it is narrated that the Prophet (peace and blessings of Allaah be upon him) said: ‘Whoever equips a warrior has taken part in the military campaign, and whoever takes care of his family (in his absence) has taken part in the military campaign.’ Undoubtedly the mujaahideen who are striving for the sake of Allaah are in the greatest need of material help, and spending on them is better than spending on a voluntary Hajj, because of the two hadeeth quoted above and others.”
And he said:
“It is better for the one who has already done the obligatory Hajj and ‘Umrah to spend the equivalent of the cost of a voluntary Hajj and ‘Umrah helping the mujaahideen who are fighting for the sake of Allaah, because jihad as prescribed in sharee’ah is better than a voluntary Hajj and a voluntary ‘Umrah.”
Shaykh Ibn Baaz was asked: Is it better to donate money to build a mosque or to do Hajj on behalf of one's parents?
“If there is an urgent need to build a mosque, then he should donate his Hajj expenses voluntarily to help build the mosque because that will be of greater and more lasting benefit and will help the Muslims to establish regular prayers in congregation.
But if the need to spend the expenses of Hajj – I mean a voluntary Hajj – on building the mosque is not urgent because there are others who can help with that who are not planning to go for Hajj, then his spending on Hajj on behalf of his parents, whether he does that himself or appoints a trustworthy person to do that, is better in sha Allah. But he should not do one Hajj for both of them, rather he should do one Hajj for each of them separately.”
See Majmoo’ Fataawa al-Shaykh Ibn Baaz, 16/368-372
Shaykh Ibn ‘Uthaymeen said:
What we think is that spending the money on jihad is better than spending it on a voluntary Hajj, because the naafil of jihad is better than the naafil of Hajj.
Fataawa Ibn ‘Uthaymeen, 2/677.