One who needs to use the facilities to a point where he is unable to hold himself in for an extended period of time, is not allowed to daven. If one davened when they had to go to the bathroom and couldn't contain themselves for a significant amount of time, the halacha is that they must daven again (siman 92).
The gemara cites a machlokes about one who urinated during their tefillah and concludes that if they delayed כדי לגמור את כולה, they must start from the beginning, but if the time delay was minor, we pasken like the more lenient opinion that one can simply continue from where they left off.
What is the halacha if one took a break in middle of shemone esrei to use the bathroom? If they couldn't contain themselves it is clear that the tefillah would be a toeiva anyway, so they would have to daven again. But what if they can contain themselves when they began but in the middle got a sudden urge to use the bathroom and did so, can they continue from where they left off or must they start again from the beginning?
Surprisingly, the Shulchan Aruch doesn't deal directly with this question. The Biur Halacha (92:2, yoser tov) suggests that this would be dependent on the way the rishonim learn our gemara. The gemara considers one who urinates during his shemone esrei to be גברא חזיא ותפלתו תפלה - the person is fit and therefore the tefillah is valid. This is the opinion that we follow l'halacha. However, depending on how we understand the term גברא חזי it may or may not apply to one who defecates. The Rosh says that when one urinates they are technically fit to daven even while they are urinating (on a torah level), therefore it isn't considered a hefsek. Since defecating is definitely not allowed m'doraysa while one is davening, and would turn the tefillah into a to'eiva, it would constitute a hefsek even without a significant time lapse. However, the Biur Halacha points out that Rashi interprets the phrase גברא חזי not to refer to the ability to daven while urinating, rather to refer to the state of being of the individual prior to urinating. Meaning, until he began urinating he was fit to daven so all the tefillah that was recited prior to urinating should be acceptable therefore we allow him to continue from where he left off. This applies equally to one who defecates during davening. Since until that point he was fit to daven (assuming that at the start of the davening he didn't have to go that badly), he can continue from where he left off.