Our Gemara on Amud Beis quotes a verse and idea about God’s presence even during our most impure states:
״הַשּׁוֹכֵן אִתָּם בְּתוֹךְ טוּמְאֹתָם״, אֲפִילּוּ בִּשְׁעַת שֶׁהֵן טְמֵאִים — שְׁכִינָה עִמָּהֶם.
“That dwells with them in the midst of their impurity, (VaYikra 16:16)” this teaches that even when the Jewish people are impure, the Divine Presence is with them.
There are two thoughts from Rav Tzaddok HaCohen Milublin that amplify this concept and inspire. In רסיסי לילה) יט:א) he contrasts the way that Hashem runs the world from the outside of a person, or from the inside. Thus when it states in Mishley (21:1), the mind of a King is in Hashem’s hand, it means that God controls the actions of the world leaders, it means from the outside. That is, God imposes His will upon the leaders, channeling and guiding history as it unfolds. However, regarding the Jewish soul, God’s influence comes from the inside. God dwells inside the Jewish people even in their greatest impurity. This is less a feature of God and more a feature of the Jewish soul. Namely, that even in its worst state, the soul truly wants to do God’s will on some deep level. Thus, God does not need to control the mind of a Jew from the outside, rather He merely needs to allow the inside forces of good to be unleashed. bThis is what it means that God dwells amongst the Jewish people in in their impurity.
In another area, Rav Tzaddok discusses this concept of God dwelling inside the Jewish people differently (שם כד:א). He states that the greatest enlightenment occurs after darkness. For example, the Mishkan was only established after the sin of the golden calf. Or, the preparations for the building of the temple in King David’s time only occurred first after the arc was captured by the Philistines. He even goes on to say, that there is a reflexive balancing out, what is called an Enantiodromia, of different forces as they return back to the middle. Thus, Abraham who represented love and kindness, needed to be followed by his son Isaac, represented strict law and harshness. Same with Moshe being followed by Aharon.
And so it often is, that in periods of great darkness, there is an impulse and a drive to return back to God, allowing God to manifest Himself with greater intensity. This is what it means that God wells amongst the Jewish people in their impurity. It is not merely a consolation -- it is a dynamic. There are forces during the dark times that are manifested and harnessed in order to return back to God.
Translations Courtesy of Sefaria