If a woman's husband dies whilst she is on Hajj, one of two scenarios must apply:
The news of her husband’s death reached her before she left her home for Hajj, in which case it is not permissible for her to go out for Hajj.
Ibn Qudaamah (may Allah have mercy on him) said: If she still has to do the obligatory Hajj, and her husband dies, she has to observe ‘iddah in her house, even if she misses Hajj, because otherwise she will miss her ‘iddah when it is obligatory for her, whereas Hajj may be done in some other year. End quote from al-Mughni, 8/135.
Shaykh al-Islam Ibn Taymiyah (may Allah have mercy on him) was asked about a woman who had resolved, along with her husband, to do Hajj, then her husband died in Sha‘baan; is it permissible for her to do Hajj?
He replied: She should not travel for Hajj during the ‘iddah following her husband's death, according to the view of the four imams. End quote. Majmoo‘ al-Fataawa, 34/29.
For more information please see the answer to question no. 45519.
The second scenario is if the news of her husband's death reaches her after she has set out for Hajj. In this case it depends on her circumstances. If she is close, meaning that she has not travelled the distance after which prayers may be shortened, then she should go back and observe ‘iddah in her husband's house. If she has passed the distance after which prayers may be shortened, then she should continue her journey and she does not have to go back.
Ibn Qudaamah (may Allah have mercy on him) said in al-Mughni (8/134-135): If she sets out, and her husband dies on the road, then she should go back if she is still close (to home), because she still comes under the rulings of one who is not travelling. But if she has gone far in her journey, Maalik says that she should go back so long as she has not entered ihram, but the correct view is that one who has gone far away should not return.
The view that it is obligatory to go back if she is still close is the report narrated from Sa‘eed ibn al-Musayyab who said: Some husbands died when their wives were doing Hajj or ‘Umrah, and ‘Umar sent them back from Dhu’l-Hulayfah, so that they could observe ‘iddah in their houses. If it is possible for the woman to observe ‘iddah in her house before she has gone far, then she must do so, such as if she has not yet left the built-up area [of her city]. However, the one who has travelled far is not obliged to come back, because it will cause her difficulty and will require her to travel, so it is similar to the case of one who has reached her destination.
When the one who has travelled far comes back, if she still has anything left of her ‘iddah, she must observe it in her husband's house, and there is no difference of opinion concerning that as far as we know, because it is possible for her to observe ‘iddah there, so she has to do it as if she had not travelled and left it. End quote.
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) was asked: If a woman goes out for Hajj, and after she reaches Jeddah she hears that her husband has died, should she complete Hajj or should she observe ‘iddah?
He replied: She should complete Hajj, because if she goes back, her return will involve travelling, but if she stays she will still be travelling, so she should complete Hajj, especially if it is obligatory for her, then she should go back. Even if it is a naafil Hajj, she should complete it. End quote from Majmoo‘ Fataawa Ibn ‘Uthaymeen, 21/58.
To sum up:
If a woman has gone for Hajj then she hears that her husband has died whilst she is on Hajj, she is not obliged to go back home, because that will involve difficulty, but when she goes back after Hajj, if there is anything left of her ‘iddah, she should complete it in her husband’s house.
And Allah knows best.