The Gemara itself frames the bathroom experience as an occasion for avodah: one who enters addresses the angels who accompany him and asks them to wait, and upon exiting recites a brachah praising God for forming the human body with wisdom, full of orifices and cavities whose proper function sustains life (Berakhot 60b:1-2). This brachah, known as Asher Yatzar, is codified as standard halacha, to be said upon leaving the bathroom in gratitude for the wondrous design of the body (Shulchan Arukh, Orach Chayim 6:1), and is likewise detailed by Shulchan Arukh HaRav, Orach Chayim 6:1-3, while Mishneh Torah, Prayer and the Priestly Blessing 7:8 rules that the brachah is recited specifically when one has actually entered a bathroom and relieved himself.
Peninei Halakhah, Berakhot 13:12 draws out the point that this brachah is not a perfunctory formula but a genuine expression of thanks for the hidden wisdom in the body's design — the hollow organs, the openings for breathing and eating, and the separate system for expelling waste — and adds that one should have proper intention in reciting it, which may even be a merit for continued health and long life.
The concern for bodily function is tied to broader religious value: Mishneh Torah, Human Dispositions 4:1 teaches that bodily health and wholeness is itself a way of serving God, since illness prevents clarity of mind for knowing the Creator, and therefore one should not delay relieving himself even a moment.
Alongside turning the bathroom experience into an occasion for brachah, the sources also mark its boundaries: Shulchan Arukh, Orach Chayim 85:2 forbids even contemplating words of Torah in the bathroom or any filthy place, permitting only mundane speech or divine epithets rather than sacred names. Related passages in the Gemara about the bathhouse sharpen this distinction: Shabbat 10a:9-11 permits prayer and Torah reading in the outer, clothed area of a bathhouse but not further in, and Shabbat 10b:1-2 explains that the bathroom itself is excluded from this leniency entirely because it is inherently repulsive, unlike a bathhouse before one actually bathes.
A later responsum, B'Mareh HaBazak Volume III 3:2, addresses a related practical question — whether one may speak in a bathroom that also contains a bath or shower — and rules that mundane speech is permitted there when one has not entered to relieve himself, and even without a bath, speech is permitted when one is not currently doing so.
Az Nidberu (Az Nidberu 14:42) takes up the related question of whether mitzvot such as standing for an elder or giving charity may be performed inside a bathhouse, examining whether performing a mitzvah there should be treated like Torah learning for purposes of the restrictions on sacred activity in such a place.
Revavos Ephraim (Sh"t Rivevot Efraim 1:8) considers a related question of bringing food into a bathroom, including whether food that has been placed in such a space may still be eaten or used for a mitzvah, suggesting that the underlying concern may be one of dignity rather than an absolute prohibition, and that it may apply only to food left uncovered.
Broader messages about sanctifying even mundane physical acts reinforce this theme: Mesillat Yesharim teaches that a person's soul should remain attached to God even while engaged in necessary physical actions (Mesillat Yesharim 26:2), the Shenei Luchot HaBerit urges that the soul govern the body even in its basic physical functions so that the mundane be elevated toward holiness (Shenei Luchot HaBerit, Shaar HaOtiyot, Derech Eretz:8), Tanya describes the value of restraining one's impulses even in fully permitted matters as a means of serving God (Tanya, Part I; Likkutei Amarim 27:11-13), and the pasuk instructs that one know God in all his ways (Mishlei 3:6).
Finally, the requirement of washing hands upon leaving the bathroom, codified in Shulchan Arukh, Orach Chayim 4:1 and elaborated by Mishnah Berurah 4:1-4, situates the bathroom experience within a larger halachic framework of purification tied to renewal of the body each day.