The Torah tells us that on the morning following the destruction of Sedom and Amorah and the other wicked cities, Avraham Avinu arose early and went back to the place where he had previously stood in the presence of Hashem (בראשית י"ט:כ"ז). The Gemara in Berachos (דף ו:) explains that this is where Avraham had davened to Hashem, and we therefore see that Avraham had a specific set place, a Makom Kavua, for his Tefillah. The Gemara thus derives that anyone who establishes a fixed place for davening will be helped by the G-D of Avraham. On the next page (שם דף ז:), the Gemara adds that when one has a fixed place to daven, his enemies will fall away.
The Gemara earlier (שם דף ו.) indicates that it is specifically in the Beis Haknessess that one's Tefillos are heard because this is where Hashem is to be found; it seems clear that this is where one's Makom Kavua for davening should be. The Yerushalmi in Berachos (פרק ד' הלכה ד', דף ל"ה.) likewise states that one must daven in a place set aside for Tefillah, based on a Posuk elsewhere in the Torah (שמות כ:כ"א). Therefore, the Gemara in Sanhedrin (דף י"ז:) says that it is improper for a Talmid Chacham to live in a city which has no Beis Haknesses. The Tosefta in Bava Metzia (פרק י"ב הלכה י"א) says that the residents of a city may compel one another to build a Shul; the Rambam (פרק י"א מהל' תפילה הלכה א') writes that wherever ten Jews live, they must set up a Beis Haknesses where they can come together to daven.
The Shulchan Aruch (או"ח סימן צ' סעיף ט') therefore rules that one must make every effort to daven in a Shul; the Magen Avraham (שם ס"ק ט"ו) adds that even if one can get a Minyan together in his home, it is still preferable to daven in a Shul. Interestingly, the Beis Yosef (או"ח שם בד"ה לא יתפלל) quotes from the Geonim that even if one can't make it to Shul to daven with the Tzibbur, one should still go to daven in the Shul building, even as a Yachid, because it is a place set aside for Tefillah. The Lechem Mishneh (פרק י"א מהל' תפילה הלכה א') asserts that this is the opinion of the Rambam too, and the Shulchan Aruch (שם) rules accordingly. The Magen Avraham (שם ס"ק ל"ג) notes, however, that if one must daven at home, one should still have a set place there for his Tefillah.
The Mishnah Berurah (שם ס"ק כ"ח) writes that if one has a choice of several Shuls in which to daven, one should go to the one which has a large crowd because of the idea of ברוב עם הדרת מלך, that Hashem is glorified more by a larger multitude. Even if there are wicked people who are part of the Tzibbur, the Ba'er Heitev (שם ס"ק י"א) quotes that one should still daven in Shul with them. The Shaarei Teshuvah (שם ס"ק ב'), however, discusses just how many such people may be in the Shul before one ought to leave it. He then adds, as does the Mishnah Berurah (שם) that if because of the crowd there is a great deal of disturbance and confusion and it is thus difficult to hear the davening and the Kerias HaTorah, it is then preferable to find a Minyan, albeit a much smaller one, somewhere else. Rav Moshe Feinstein, however, (שו"ת אגרות משה, או"ח חלק א' סימן צ"ט ) advises one to be hesitant about leaving a Shul, saying that by staying with a Shul whose members perhaps do not act properly, one can influence these people and show them the right path, and thus should not leave. This is, of course, provided that the actual running of the Shul is done in accordance with Halacha.
Once one is in a proper Shul, is it necessary to have a Makom Kavua there? Rabbeinu Yonah in Berachos (דף ג: בדפי הרי"ף בד"ה כל הקובע) says that since the entire Shul is a Makom Tefillah, one need not be insistent upon always sitting in the same place; the whole Shul is one's Makom Kavua. However, the aforementioned Yerushalmi states that even within the Shul one must specify a place for himself to daven, based upon the practice of Dovid HaMelech (שמואל ב' ט"ו:ל"ב), who would always bow and worship in the same place. This view seems to be accepted by the Rosh in Berachos (פרק א' סימן ז'). The Rambam (פרק ה' מהל' תפילה הלכה ו') likewise stresses that one should always daven in the same Makom Kavua; the Hagahos Maimoniyos (שם אות י') spells out that even within the Shul itself one should not change one's place. This is the opinion accepted by the Shulchan Aruch (או"ח שם סעיף י"ט) which states that it is not sufficient to simply choose a Shul, but even within the Shul one must have a Makom Kavua. It is worth noting, however, that the Magen Avraham (שם ס"ק ל"ד) suggests that anywhere within 4 Amos of one's usual place is still considered one's Makom Kavua; the Mishnah Berurah (שם ס"ק ס') accepts this view.