The gemara says that one cannot destory a shul until the replacement is rebuilt. The gemara quotes 2 reasons for this issur: 1. Negligence - maybe an o'nes will occur (rashi) that prevents the building of the new shul. 2. No place to daven - in the meantime there won't be anywhere to daven. The Shulchan Aruch (152) paskens like the first reason, therefore it would be assur even if there were another place to daven. But, the m.b. (5) says that when there is another shul in town that can fit the entire tzibur, the taz is matir, but the magen avrohom is machmir. The biur halacha explains that one can rely on the taz since we are only dealing with an issur d'rabonon, and many rishonim are matir even according to the first reason when there is an established shul to daven in, not just a place to daven (tosafos). Based on this, a shul has a heter to close in order to rebuild so long as there are other shuls in the community that can hold all the members of the one that is rebuilding.
The gemara says that the entire issur only applies when the shul is in good condition, but if it starting to rot and therefore not functional in it's present state, one can destroy it to build another. The gemara says that the only heter that hurdos had to rebuild the beis hamikdash (based on baba ben buta's advice), was that it was starting to rot. The m.b. (2) proves from here that even if the intent is to make a much nicer shul, it is assur so long as the first one is still functional. However, the Taz (quoted in m.b. 9) is liberal about the definition of "rotting". The Taz holds that when the first shul is too far from where the community lives such as outside the wall of the city - אין לך תיוהא גדולה מזו, and it can be rebuilt in a more appropriate location. Similarly, Tosafos explains that a summer shul in winter or visa versa, can qualify as "tiyuha" and allow one to rebuild.
It is important to note that the entire sugya of destroying a shul, is only considered an issur d'rabonon because it is being done for constructive purposes i.e. to rebuild another in it's place or elsewhere. BUT, when the shul is being destroyed for a destructive purpose, it is an issur d'oraysa, at the m.b. (11) points out - לא תעשון כן לה' אלקיכם. The biur halacha explains that this not only applies to items that are attached to the ground, but even destroying movable items such as the bima and amud are an issur d'oraysa. The maharam padawa is matir that the removal of the tangible items from the shul is not a violation of this issur, since it is not destroying the actual structure (unlike the removal of bricks).