The Gemara in Masechet Shabbat (11) warns against constructing houses that are taller than the synagogue: "Any city whose rooftops are taller than the synagogue will eventually be destroyed." The question arises as to whether this is the accepted Halacha, and, if it is, how we may justify the prevalent practice to allow building homes that extend higher than the local synagogue.
The Shulhan Aruch indeed codifies this prohibition (Orah Haim 150:2). The only qualification he mentions is that this Halacha refers not necessarily to a house’s roof, but rather to the highest functional area in the house. Sloped roofs, which most of today’s houses have, are not usable, and they therefore may extend higher than a synagogue. The highest usable area in houses today is usually the attic, and thus the attics may not be taller than the town’s synagogues.
In any event, the question remains as to why this Halacha is not commonly observed, as many people live in buildings that extend higher than the synagogues in their town.
The Magen Abraham (Rabbi Abraham Gombiner of Poland, 1637-1683) commented that this prohibition perhaps does not apply in a town inhabited by both Jews and gentiles. If some non-Jewish homes in the town in any event extend higher than the synagogue, then the synagogue will not be the tallest building regardless of how high the Jews construct their homes. For this reason, perhaps, Jews over the centuries did not observe this prohibition, since they lived among gentiles who built buildings that were taller than the synagogues.
Another possible justification emerges from a comment of the Meiri (Rav Menahem Meiri, 1249-1315), who held that the prohibition does not apply if residential buildings are built higher due to space limitations, rather than for show. Very often (as is the case today), tall buildings are built because the town does not have enough space to expand outward, and so the residents expand "upward" by building additional levels to their homes. If this is the motivation behind the construction of a tall building, the Meiri maintains, then the building may be built higher than the synagogue, and this does not infringe upon the honor of the synagogue.
As for the final Halacha, Hacham Ovadia Yosef (as cited in Yalkut Yosef) rules that those who act leniently in this regard certainly have a basis on which to rely. Although the Hid"a (Rav Haim Yosef David Azulai, 1724-1806), the Kaf Ha’haim (Rav Yaakob Haim Sofer, 1870-1939), and many other authorities disputed the Meiri’s position, his view may nevertheless be invoked as a possible justification for the widespread practice of allowing residential buildings to extend higher than the synagogue. Thus, while it is certainly preferable to ensure that one’s home does not extend higher than any of the synagogues in the town, those whose homes do extend higher have a Halachic basis on which to rely, and do not have to lower their homes.
Summary: One should preferably not build a house whose attic is higher than one of the local synagogues, though those whose houses do extend this high have a Halachic basis on which to rely.