BAL TESHAKTZU [Tefilah: Bal Teshaktzu]
Gemara
(Rav Achai): If one urgently needs to excrete and holds it in, he transgresses "v'Lo Seshaktzu (do not make yourselves detestable)";
Berachos 23a (Beraisa): If during Shemoneh Esreh one (uncontrollably) started to urinate, he waits until the urine stops, then resumes his Shemoneh Esreh.
(Rav Chisda or Rav Hamnuna): He returns to the beginning of Shemoneh Esreh;
(The other of Rav Chisda and Rav Hamnuna): He returns to the place he stopped.
(Rav Ashi): If he delayed long enough to finish, all agree that he returns to the beginning. They argue when he did not delay long enough to finish.
The first opinion holds that he was unfit to pray from the start, therefore his prayer is invalid. The second opinion holds that he was not unfit, so his prayer is valid.
(Beraisa): If one feels a need to defecate, he may not pray. If he prays, his prayer is an abomination.
Version #1 (Rav Zevid or Rav Yehudah): This is only if he cannot wait (before relieving himself), but if he can wait, his prayer is valid.
(Rav Sheshes): He must be able to wait the time it takes to walk a Parsah (about four kilometers, i.e. 72 minutes).
Version #2 (continuation of Beraisa): This is only if he cannot wait, but if he can wait, his prayer is valid.
(Rav Zevid): He must be able to wait the time it takes to walk a Parsah.
(R. Shmuel bar Nachmani): If one feels a need to move his bowels, he may not pray -- "Hikon Likras Elokecha Yisrael."
Rishonim
Rif (Berachos 14a): If one feels a need to defecate, he may not pray. If he prays, his prayer is an abomination, i.e. if he cannot wait. If he can wait the time to walk a Parsah, it is permitted.
Rosh (Berachos 3:24): The Rif's text says that if he can wait, he may pray. I disagree, for a Beraisa afterwards forbids l'Chatchilah if one feels a need. Most texts say that if he can wait, i.e. the time to walk a Parsah his Tefilah is valid.
Rambam (Hilchos Ma'achalos Asuros 17:31): One may not delay at all to eliminate or urinate. If he does, he makes his body detestable, brings illnesses upon himself, and he is liable for his own soul. One should accustom oneself [to go] at regular times, lest he need to distance himself or make himself detestable.
Rambam (Hilchos De'os 4:1): A healthy body is part of Hash-m's ways. One cannot understand when he is sick, so he must distance from things that weaken the body. Whenever one must urinate or eliminate, he should not delay at all.
Rambam (Hilchos Tefilah 4:10): One who needs to eliminate may not pray. If he did, it is an abomination; he must pray again after eliminating. If he could have walked a Parsah, his Tefilah was valid. L'Chatchilah one may not pray until checking himself.
Tosfos (22b DH Af): If he prayed, it was an abomination and "the crooked cannot be fixed." The Ri says that he prays again.
Gra (OC 92:2): The Gemara connotes like the Ri. It seems that if he could wait, his Tefilah was Tefilah. This implies that if he could not wait, his Tefilah was not Tefilah (it is as if he did not pray).
Rashba (1:131): If one checked himself before Tefilah and felt no need, and in the middle he got an urge, he does not interrupt, even if the urine is dripping, like the Gemara says. To distance oneself from urine is only mid'Rabanan; to avoid interrupting Tefilah, we rely on Torah law. All the more so, if he can hold it in until he finishes, he does so. The same applies to Gedolim. During Kri'as Shema, if he can wait until he finishes, he does so. He may distance and urinate if he wants.
Poskim
Shulchan Aruch (OC 92:1): If one needed to eliminate, he may not pray. If he did, it is an abomination and he must pray again. This is only if he could not wait the time to walk a Parsah.
Beis Yosef (DH u'Mah she'Chosav v'Hani): Since his prayer is an abomination, clearly he must pray again.
Magen Avraham (1): The Gemara and Rosh connote that he must pray again only if he felt a need for Gedolim. The Bach says that even if in the time to eliminate he will miss the time to pray, and he could delay the time for a Parsah, he may not pray until relieving himself. I say that in such a case he should rely on the Rif, who permits.I found like this in Siman 80.
Mishnah Berurah (2): Some say that he prays again also if he needed to urinate. Some disagree.
Mishnah Berurah (3): It takes 72 minutes to walk a Parsah.
Mishnah Berurah (4): If at the beginning he could wait this long, even if he eliminated immediately afterwards, b'Di'eved he was Yotzei.
Mishnah Berurah (5): It is better to pray alone with a clean body than with the Tzibur when he feels an urge. However, if he would lose praying within the time, it is better to pray if he could wait 72 minutes.
Kaf ha'Chayim (1): If one does not pray until he eliminates, he receives reward for preparing a clean body to stand in front of Hash-m.
Mishnah Berurah (6): If he said Kri'as Shema when he felt an urge, b'Di'eved he was Yotzei.
Mishnah Berurah (7): If one teaches Torah to a Rabim, and in the middle he has an urge, even for Gedolim, he may delay it. Bal Teshaktzu is mid'Rabanan, and Kavod ha'Briyos overrides it.
Kaf ha'Chayim (6): A Shali'ach Tzibur may say Chazaras ha'Shatz even if he has an urge, due to Kavod ha'Briyos. The same applies to one called to have an Aliyah.
Kaf ha'Chayim (7): One may delay due to Kavod ha'Briyos if he is amidst people and he does not want to make them rise, or to appear to walk on their heads. One should prepare in advance to be able to pray in cleanlinesss and not need to burden others.
Shulchan Aruch (ibid): If he could wait, b'Di'eved he was Yotzei, but l'Chatchilah he may not pray until he checks himself well.
Beis Yosef (DH): We follow the Rambam, R. Yonah and Rosh, who all disagree with the Rif.
Gra (4): 'His Tefilah was Tefilah' connotes b'Di'eved. However, the Yerushalmi connotes like the Rif.
Rema: One who needs to eliminate may not say Divrei Torah while his body is disgusting due to the excrement.
Source: Hagahos Maimoniyos (Hilchos Tefilah 4:8, cited in Darchei Moshe 1).
Shulchan Aruch (2): If during Shemoneh Esre one felt an urge to eliminate, he waits until he finishes, and does not interrupt. If he had an urge during Kri'as Shema and its Berachos, whether for Gedolim or Ketanim, he reads normally.
Beis Yosef (DH veha'Rashba, citing the Rashba): If one felt a need from the start, and prayed without relieving himself, it is an abomination if he could not wait the time to walk a Parsah. If he got an urge only in the middle, he does not interrupt (Shemoneh Esre). If he was in Kri'as Shema and its Berachos, and he can hold it in, he reads normally.
Darchei Moshe (1): This is difficult, for Terumas ha'Deshen (16) connotes that it is forbidden to delay for Gedolim or Ketanim, and one must interrupt. Perhaps here we do not discuss a great urge to which Bal Teshaktzu applies, but he cannot wait a Parsah.
Gra (6): The Gemara says that if urine was dripping... This implies that he does not interrupt to go elsewhere, and all the more so he can wait until he finishes, and similarly regarding Gedolim.
Gra (7): In Kri'as Shema, he may interrupt if he wants.
Rema: This is only if he does not need to eliminate so badly that Bal Teshaktzu does applies. If it applies, it is better to interrupt.
Magen Avraham (2): Even the Terumas ha'Deshen agrees in a Tzibur, for Kavod ha'Briyos overrides mid'Rabanan laws. Even one who prays alone should rely on the Rashba, for only Piku'ach Nefesh permits interrupting Tefilah (Semag). Bal Teshaktzu is mid'Rabanan; Chachamim did not require interrupting Tefilah for it. Perhaps Terumas ha'Deshen discusses only gas, for which one distances four Amos, which is not a Hefsek.
Mishnah Berurah (8,9): After Tefilah he must relieve himself immediately. He may not wait to answer Kedushah, for it is a different matter. Likewise, if an urge arose during Kri'as Shema, he may finish Kri'as Shema, but he may not begin Tefilah, for it is a different matter.
Mishnah Berurah (11): If Bal Teshaktzu applies, one may interrupt if he wants. He need not start again from the beginning, since he checked himself beforehand, unless while relieving himself he delayed the time for a full Shemoneh Esre.
Kaf ha'Chayim (8): It is better not to interrupt, if possible. It is better to urinate without moving and wait for it to finish. If it would drip to the ground and he would be embarrassed, he may go away and return.
Shulchan Aruch (ibid): If he wants to distance and urinate, he may.
Kaf ha'Chayim (9): One may not do so for Gedolim, even in Pesukei d'Zimrah after Baruch she'Omar, unless Bal Teshaktzu applies. The Magen Avraham would agree that in this case, one interrupts Kri'as Shema.