Praise be to Allah.
The basic principle is that a nafl 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 nafl Hajj, such as if the money is spent on jihad for the sake of Allah, or for calling people to Allah (da’wah), or on needy people, especially if they are relatives.
Shaykh al-Islam Ibn Taymiyah said in al-Ikhtiyarat (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 ‘aqiqah (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 haram things, offers all five daily prayers, speaks the truth, fulfils trusts and does not violate the rights of anyone.”
Shaykh Ibn Baz (may Allah 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 Allah Alone and does these rituals in the manner prescribed in shari’ah. The Prophet (peace and blessings of Allah 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-Bukhari, 1773; Muslim, 1349)
And he (peace and blessings of Allah be upon him) said: “‘Umrah in Ramadan is equivalent to Hajj.’ (Narrated by al-Bukhari, 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 mujahidin who are striving for the sake of Allah, because when the Prophet (peace and blessings of Allah be upon him) was asked which deed is best, he said, ‘Belief in Allah and His Messenger.’ He was asked, ‘Then what?’ He said, ‘Jihad for the sake of Allah.’ He was asked, ‘Then what?’ He said, ‘An accepted Hajj.’ (Narrated by al-Bukhari, 26; Muslim, 83)
So Hajj comes after jihad. What is meant here is the nafl Hajj , because the obligatory Hajj is one of the pillars of Islam, so long as one is able to do it. In al-Sahihayn it is narrated that the Prophet (peace and blessings of Allah 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 mujahidin who are striving for the sake of Allah are in the greatest need of material help, and spending on them is better than spending on a voluntary Hajj, because of the two hadith 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 mujahidin who are fighting for the sake of Allah, because jihad as prescribed in shari’ah is better than a voluntary Hajj and a voluntary ‘Umrah.”
Shaykh Ibn Baz 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 Majmu’ Fatawa al-Shaykh Ibn Baz, 16/368-372)
Shaykh Ibn ‘Uthaymin said:
“What we think is that spending the money on jihad is better than spending it on a voluntary Hajj, because the nafl of jihad is better than the nafl of Hajj.” (Fatawa Ibn ‘Uthaymin, 2/677)
And Allah knows best.