In Keser Shem Tov the Baal Shem speaks of the inyan of Machshovos Zoros, “Foreign Thoughts,” that enter the mind during davening and other aspects of avodas Hashem. In short, he says there that they all have their source in the Aibishter, since there is no place lacking His presence, and therefore we can elevate those thoughts. He states also that there are thoughts which are to be pushed away, and that the difference can be determined through the nature of the thought, which requires some amount of training of the intuition.
Many Sifrei Chassidus talk about this inyan, because it is common to most people. The Baal HaTanya says that even Tzaddikim are inclined to Machshovos Zoros, such as when davening with the tzibbur (congregation), hence why Tzaddikim should remain isolated much of the time, so they don’t pick up the thoughts and feelings of others. The Ateres Tzvi says in Sur MeRa that various forms of these thoughts have their roots in the highest of holy places, and that we return them there through davening with kavonno, particularly with the Kabbalistic Kavonnos, for those who are able to do this. Rebbe Nachman states that the best way to deal with intrusive thoughts in tefillah is through simply focusing on the holy words of the prayers, and through this the thought will go away.
The Baal Shem HaKodosh speaks also of Kabbolas Ohl Malchus Shomayim, Accepting the Yoke of the Heavenly Kingdom, particularly in regards to repeating words of Sh’ma if one didn’t say it properly with kavonoh. He brings that through repeating words, even if it is to repair saying without kavonoh, shows that one doesn’t believe Hashem truly fills the world with His Presence, and there is nowhere that He isn’t. Even if words of tefillah are said without kavonoh, the reason is to make a Tikkun in some other place that perhaps you can’t focus on yourself.
Now on the daily level, this is a huge problem for most people. It is easy to fall into the trap of saying “אית אתר פנוי מניה” “There is a place that is turned away from Him,” chas v’sholom, Rachmono litzlon. There is no place in the entirety of Hashem’s Creation that is able to exist without His constant influence and infusion of Light. We see various forms of meshigas (craziness) in the news, or in our own personal lives, then come to think that Hashem isn’t also there. The reality is that Hashem completely fills the entirety of His Creation and encompasses it all. The purpose of everything is that Hashem wishes to grant us His kindness, as it says in the Gemoro, the Zohar, and other places; as well as to bring us to the Y’Mos HaMoshiach, the time of Moshiach, to accomplish the purpose of Creation. From the Seder B’reishis, which we say every night by Kiddush, one can learn the concept of Y’Mos HaMoshiach, and realize how Shabbos is M’ain Olam Haba, המבין יבין. Ultimately, Hashem runs absolutely everything, there is no place in the world without Him. Even though we have a concept of Tumah, and various things that are forbidden from the Torah and Chazal, even those things have Netzotzos HaKedoshim, those Divine Sparks, that are specifically elevated through our avoiding them.