Ashkenaz say Bameh Madlikin, and those who daven Nusach Sefard say Kegavnah... Why this distinction?
And if one is praying in a shul that does not say his Nusach, what should he do?
Various reasons are given for saying Bameh Madlikin. One is to remind people of the things that need to be done before Shabbos (including the proper wicks and oils to use); however, this is not relevant where people pray Maariv after dark anyway, as is generally the chassidic custom, and they therefore omitted it. (Aruch Hashulchan 270:2; R' Avraham Chaim Naeh, Piskei Hasiddur 129)
Also, another reason given for saying Bameh Madlikin is to prolong the services, so that people who came late won't have to catch up. Here too, when Maariv is started after dark, we assume that fewer people will be late (since they have to have stopped working before sunset); furthermore, since Nusach Sefard and Ari follow the Arizal in adding Mizmor Ledavid, half-Kaddish, and Barchu before Aleinu, then these additions serve the same purpose. (Piskei Hasiddur loc. cit.)
As for Kegavna, the custom to say it comes from various recensions of the Arizal's Siddur, so naturally again it is found in Nusach Sefard and Ari, which more-or-less follow his directives. But I don't think it was designed to replace Bameh Madlikin (though possibly it did so simply in order to keep Maariv from being too long).
As for what to say in a shul that has the opposite custom: I would think that since it's said privately anyway, there should be no problem saying whatever you usually say. (And after all, the practitioners of each custom have no "ideological" reasons against the other; chassidim are allowed to learn Mishnayos, and Litvaks are allowed to recite a section of the Zohar.)
I want to correct and some things that appear in Alex's response above.
1) Saying/learning Bameh madlikin at the beginning of Shabbos is a minhag kadmon, an ancient minhog. That means very old, I don't know exactly, but I could see it being over one thousand years old, and maybe even a good deal longer than that. As stated above Sepharadim say it as well as Ashkenazim, so that shows you that it is not just a narrow minhog, but was the general minhog, even in places like Poland and Hungary, before the Hassidic movement.
2) Originally it was said after shmoneh esreh in maariv of Shabbos/Friday night, not before borchu. It is still recited in that part of the davening by Yekkes and Oberlanders, e.g. Khal Adas Yereim Vien Shul in Borough Park, Brooklyn, Ashkenazim from Western and Central Europe.
3)The Chayei Adam, R. Avrohom Danzig of Vilna, advocated moving it to before maariv/borchu, the logic being that then one could still fix a problem addressed in the mishnayos, if Shabbos has not started. In Lita, Eastern Europe, based on this, it was shifted to before borchu/maariv.
4) In kehillos of Western Europe, however, the old minhog of saying it later, after shmoneh esreh did not change. They did not change their tradition based on the sevara of the Chayei Adam.
5) There are various reasons for saying/learning bameh madlikin, beyond the sevara assumed by the Chayei Adam. Some say that this perek is learned/recited to stress that we do have lights on Shabbos, e.g. set up, and do not sit in the dark like the Karaites (keraim). Also, in general, there is an inyan to learn the halochos of a day on that day, so a perek of Maseches Shabbos qualifies for that.
6) To sum up, this is a one of the ancient minhogim of Friday night/Shabbos (like making kiddush in Shul), which is still maintained by those who faithfully follow the holy mesorah of Ashkenaz mesorah. Hakodosh boruch Hu should reward their faithfulness.
Kegavnah is a piece of Zohar, Parashat Terumah Page 135a, b that refers to the change in the world when Shabbat comes in, specifically when the words of Borchu of Maariv are said. It explains that the "oneness" of this world can bond with the true "oneness" of Above, making G-d's Throne "complete" (see Rashi on Exodus 17:16), allowing Him to "sit" upon it. This is a prequel to, and microcosm of, the "day that is completely Shabbat." Hence, this piece of Zohar says that this is "the Secret of Hashem Echad uShemo Echad." (Please note that numerically, Echad = 13. So when the Echad of Above unites with the Echad of Below, it now numbers 26, the numerical value of Y-H-V-H.) This oneness causes all forces of evil (damaged & incomplete good, lack, etc.) - and Judgement - to run away, as Good & Mercy (brought on by the Joy of Completeness) rule at this juncture. (Hence no verse of Judgement preceding Borchu like the weekday V'Hu Rachum.) It proceeds to explain how Borchu & the corresponding congregational response, is appropriate for such a moment of Unity.
Said properly, it leads straight into Borchu, with some congregations actually saying the words within the context of the Zohar passage. One can easily understand why the Lurianic tradition is to say Kegavnah.