Allah commands His servants to single Him out alone for worship and that none should be supplicated to along with Him, nor should any partners be associated with Him. As Qatadah said concerning Allah's statement,
(And the Masjids are for Allah, so invoke not anyone along with Allah.) "Whenever the Jews and Christians used to enter their churches and synagogues, they would associate partners with Allah. Thus, Allah commanded His Prophet to tell them that they should single Him out alone for worship." Ibn Jarir recorded from Sa`id bin Jubayr that he said concerning this verse,
وَأَنَّ الْمَسَاجِدَ لِلَّـهِ فَلَا تَدْعُوا مَعَ اللَّـهِ أَحَدًا (...and that masajid (mosques) belong to Allah; so, do not invoke anyone along with Allah....72:18). The word masajid is the plural of masjid. Here the word could be taken in its popular sense, that is, mosques or places of worship dedicated for the performance of prayers. In this case, it would mean that all mosques belong to Allah, dedicated to His sole worship and therefore we are not permitted to call on anyone else besides Allah, like the Jews and Christians commit shirk in their places of worship. In sum, the mosques must be kept clear of all false beliefs and vile deeds.
The word masajid could also have another sense. It could be the plural of masjad, with the letter jim carrying fath, in which case it would be masdar mimi 'infinitivity' and mean 'to prostrate or prostration'. The verse in this sense would signify that worship is reserved exclusively for Allah. It is not permitted to prostrate to anyone, because if he calls on anyone else for help, it is as though he is prostrating to him which must be avoided.
Tazkirul Quran
Maulana Wahiduddin KhanOn ayahs 23–27
The entire system of the present world has been formulated with the aim of putting human beings to the test. Therefore reality is revealed here solely to the extent of conveying the divine message. Had there been no human trial to be considered, and had the concealing veils been removed, the people would have seen that, from the angels to the righteous among the jinn, all acknowledge the god-head of God and the whole universe testifies to this.
Uthmani text of the Madinah mushaf (King Fahd Complex). Translation: Saheeh International. Tafsir via Quran.com.