Rebbe Shimon Bar Yochai explains in the translation below how a person causes the Divine Presence to dwell in them and what causes it to depart.

Come and see. "And when he saw the wagons which Joseph had sent to carry him, the spirit [Ruach] of their father Jacob revived [in Hebrew, 'vatechi']." (Gen. 45:7).

It appears as if his spirit was dead at first and wasn't able to receive a different spirit.

The word "vatechi" in our quote, means to return to live or to exist. Rebbe Shimon asks how something that had ceased to exist could be revived. Ruach is one of three levels of the soul that consists, in ascending order of spirituality, of Nefesh, Ruach and Neshama. Nefesh is the life force that surges through the body in the blood, enlivening the body and enabling it to function. Ruach is the life force that becomes a vehicle for the Neshama in a way analogous to the manner in which the body is the vehicle for the manifestation of the Nefesh. The different spirit that the Ruach can be attuned to receive is the Neshama /soul and the Shechina or Divine Spirit.

This is because the higher spirit doesn't dwell on an empty place.

The higher spirit here refers to the Shechina that doesn't dwell on a person who has no Neshama. Therefore as soon as Jacob heard the news that Joseph was alive, he must have merited the return of his Neshama implying that his Ruach was immediately revived as well.

Rabbi Yossi said that the Shechina doesn't dwell on any place that is not complete, or in a place that is blemished, or in a place that is sad. The Shechina doesn't dwell on any place that…is sad…

Rabbi Yossi comes to explain why the Shechina had not dwelled on Jacob up to this point. A person who has not acquired all three of these spiritual levels has not yet completed the work required in rectifying his sefirot, and the emotional and intellectual levels that they imply. It follows that his spiritual makeup is blemished. His misdeeds prevent his spiritual completion and are the reason he is not worthy of being a dwelling place for the Shechina. Sadness is also a reason because it is a positive command to worship G‑d in happiness. Put another way, sadness, as a state of being, blocks a person's ability to properly serve G‑d.

Rather [the Shechina] dwells in a place that is ready, a place of happiness.

A place that is ready to become a dwelling for the Shechina is a person who has genuine joy in the performance of the mitzvot/commandments. The root of joy is the sefira of bina, which is connected to the heart as well as the mind. Joy can therefore permeate a person - and starts as a mental frame of mind that influences the whole person, just as the heart influences the body. A joyous person is thus unified in mind and body and can become a vehicle for the manifestation of a higher unity - the Shechina. This cannot happen in a sad person.

Because of this, the Shechina didn't dwell upon Jacob for those years that he was sad. Those were the years that Joseph was separated from his father.

We have learned that Rabbi Elazar said in the name of Rabbi Aba that it is written "Serve G‑d with joy; come into His presence with singing." (Psalms 100:2). This emphasizes that there is no [complete] service of G‑d unless it is through joy. …Jacob literally merited to come back into the presence of G‑d through singing!

It is interesting to note that the Midrash says that the brothers didn't know how best to break the good news to Jacob. Finally Serach, the daughter of Asher, sang a song that had the words "Joseph is alive and living in Egypt". Jacob literally merited to come back into the presence of G‑d through singing!

As Rabbi Elazar said, the Shechina does not dwell on a place of sadness, as it is written [concerning the prophet Elisha when he was angry and saddened by the behavior of Yehoram King of Israel] "Now bring me a [musical] player. And it came to pass, when the player played, that the hand of the Lord came upon him". (Kings II 3:15). Why is the word "play" repeated three times? It is in order to awaken the spirit that is the completion of all and is a complete spirit [Ruach].

This is the spirit of prophecy that is sourced in the three divine names of Eh‑yeh Havayah and Ado-nai. The joy infused by the music… fully revives the soul and leads to prophecy…

The joy engendered by the music draws down the Neshama from the name Eh‑yeh, sourced in the sefira of bina and encompassing the three sefirot of keter, chochma and bina. These correspond to the will, the wisdom and the understanding of a person. The Ruach is aroused from the name Havayah dwelling in Zeir Anpin, which corresponds in man to the six emotional attributes of chesed, gevura, tiferet, netzach, hod and yesod. The Nefesh is drawn from the name Ado-nai that represents that sefira of malchut. Together, these are the ten sefirot that all become elevated in the spirit of a joyous person. The joy infused by the music, when combined with the proper meditation on these holy names and spiritual levels, in a person who has rectified his own corresponding emotional, intellectual and spiritual levels, fully revives the soul and leads to prophecy.

[Zohar, Page 216b; translation and commentary by Simcha-Shmuel Treister]