Why does shul open with a line from a man who came to curse us?
A famous rabbi had the same problem. Rabbi Shlomo Luria, known as the Maharshal, was a leading rabbi in Poland in the 1500s. He wrote that when he came to shul in the morning he skipped the first line, Mah Tovu, and started from the second. His reason: Bilaam said it, and Bilaam meant it as a curse. He added that this is not the place for it.
He was pointing to the Gemara. Rabbi Yochanan says that from Bilaam's blessings you can tell what he wanted to say. He wanted to say that the Jewish people should have no shuls and no study halls. What came out was "How good are your tents, Yaakov." He wanted to say that God's presence should not rest on them. What came out was "your dwelling places, Israel."
The same passage also has something on the other side. Rabbi Abba bar Kahana says that over time, all of Bilaam's blessings turned back into the curses he had in mind, except one: the shuls and study halls. He learns it from the Torah's own summary of the story: "Hashem your God turned the curse into a blessing for you, because Hashem your God loves you." It says curse, not curses. Only one stayed a blessing for good.
The Gemara also reports that the Sages (the rabbis of the Mishnah and Gemara) wanted to add the whole Torah section about Balak and Bilaam to the daily Shema. They did not, because it would have been too much of a burden on the community. The reason given is length, not who said the words.
In practice, the siddurim kept the line. The Eliyah Rabbah (a commentary on Jewish law from around 1700) records both sides: the siddurim say to begin with Mah Tovu, and the Maharshal skipped it. It does not answer his objection. So there are two approaches, and the common one is to say it. On Rabbi Abba bar Kahana's reading, the line you open with is the one curse that failed for good.
Teshuvot Maharshal 64,Sanhedrin 105b,Devarim 23:6,Berakhot 12b,Eliyah Rabbah, Orach Chaim 46:10I can pray anywhere. Why does it matter that I came to shul?
The last line of Mah Tovu asks for "a time of favor." On the plain level, David is asking for good timing. Rashi reads it: may the time of my prayer be a time of favor. The Radak reads it: I pray to You all the time, and maybe one of those times will be a time of favor.
The Gemara gives the phrase a specific address. Rabbi Yitzchak once asked Rav Nachman why he had not come to shul to pray. "I couldn't," said Rav Nachman. Then gather ten people at your house, said Rabbi Yitzchak. "That is too much trouble for me." Then ask the prayer leader to let you know when the community is praying, so you can pray at the same time.
Rav Nachman asked: why all this? Rabbi Yitzchak answered with a teaching on our verse: "As for me, my prayer is to You, Hashem, at a time of favor." When is a time of favor? When the community is praying. A few lines later the Gemara adds that God does not reject the prayer of the many.
The third line of Mah Tovu gets similar treatment. Abaye, one of the best-known teachers in the Gemara, said: at first I studied at home and prayed in shul. Once I heard what David said, "Hashem, I love the house where You live," I began to study in shul as well.
Some statements go further. Abba Binyamin says a person's prayer is heard only in shul. When the Shulchan Aruch (the main code of Jewish law) turns this into practice, it says a person should make an effort to pray in shul with the community. Someone who cannot get there should try to pray at the time the community is praying. That is the same advice Rabbi Yitzchak gave Rav Nachman.
So on the Gemara's reading, the "time of favor" in the last line is not something you wait around for. It is the time the community prays, which is what you are walking into.
Rashi and Radak on Tehillim 69:14,Berakhot 7b-8a,Megillah 29a,Berakhot 6a,Shulchan Aruch, Orach Chaim 90:9My shul is an ordinary room. How is that God's "house"?
The words in Mah Tovu are big: Your house, Your holy sanctuary, the place where Your glory dwells. You might be saying them in a school gym or a classroom.
Start with who said them first. Three of the five lines come from psalms of David. David never saw the Temple. God told him that his son would be the one to build it. The Radak explains that when David says "Your house," he means the house where the Ark was. Once David brought the Ark to Jerusalem, it stood inside a tent he had put up for it. Put the two together, and the "house" David loved was a tent.
Later, when Jews were exiled to Bavel (Babylonia), the prophet Yechezkel quoted God: "I have been a small sanctuary for them in the lands where they have come." Rabbi Yitzchak, in the Gemara, says what that small sanctuary is: the shuls and study halls of Bavel.
The Gemara reads Bilaam's line the same way: his "tents" are shuls and study halls. Rabbi Ovadiah Sforno (an Italian commentator, 1500s) spells it out. The tents are the study halls. The dwelling places are the shuls, set aside for God's name to rest there and for prayers to be received.
One more detail. The second line says "I bow down toward Your holy sanctuary," not "in" it. The Radak comments that a person who bows to God faces the sanctuary and bows in its direction. The Gemara makes that a rule for prayer everywhere: outside Israel you face the Land of Israel, in Israel you face Jerusalem, and in Jerusalem you face the Temple. The result, it says, is that all of Israel end up directing their hearts to one place.
A tent counted for David. On Rabbi Yitzchak's reading, a shul in Bavel counts as a small sanctuary.
Radak on Tehillim 5:8 and 26:8,Shmuel II 6:17 and 7:13,Yechezkel 11:16,Megillah 29a,Sanhedrin 105b,Sforno on Bamidbar 24:5,Berakhot 30a