The source of this blessing is in Berakhot 60b, where upon exiting the bathroom one blesses Hashem Who formed man in wisdom and created orifices and cavities, declaring it known before the throne of glory that were one of them to be ruptured or blocked, it would be impossible to survive — the text given in full in Siddur Edot HaMizrach (Preparatory Prayers, Morning Blessings 4-5).
Shulchan Arukh, Orach Chayim 7:1-3 rules that throughout the day, whenever one relieves himself, whether urination or defecation, he recites Asher Yatzar but not Al Netilat Yadayim even if he intends to learn or pray immediately, a position the Tur (Orach Chayim 7) and the Rosh (on Berakhot 9:23) discuss as well, noting that the washing blessing was instituted only for meals and prayer, not for this act; Beit Yosef, Orach Chayim 7:1-2 traces the dispute among the Rishonim over whether washing and its blessing are required for defecation specifically. Arukh HaShulchan, Orach Chaim 7:1 explains that this teaches the blessing must be said every single time one relieves oneself, not merely once in the morning like other birkot hashachar, and that it applies equally to urination as to defecation.
Mishnah Berurah 7:1-2 and Kaf HaChaim, Orach Chaim 7:1-2 address the case of one who forgot to recite the blessing before a subsequent need arose, and discuss, citing the Teshuvot Maharshal 97:2, that when someone has both an unfulfilled after-blessing from food and a need to bless Asher Yatzar, Asher Yatzar takes precedence because it is more frequent (tadir) and has no minimum measure, unlike the after-blessing which depends on an amount eaten. Shulchan Arukh HaRav, Orach Chayim 7:3 rules that if one urinated, forgot to bless, then urinated again, only one blessing is required, comparing this to one who ate, forgot Birkat HaMazon, and ate again before reciting it.
Mishnah Berurah 4:3-4 and Shulchan Arukh HaRav, Orach Chayim 6:1 explain the custom of reciting Asher Yatzar each morning immediately after Netilat Yadayim, reasoning that each day a person becomes like a new creation, so the blessing of formation is appropriate daily, while cautioning one who is uncertain whether he has relieved himself to be careful to do so right after washing in the morning. Arukh HaShulchan, Orach Chaim 4:14-15 rules that if one has not yet relieved himself, he should not recite Asher Yatzar, and it is therefore preferable to attend to one's needs first before washing and blessing.
The blessing's obligation extends to women as well: Ben Ish Hai, Halachot 1st Year, Vayetzei 1-16 states that women too must bless Asher Yatzar and should make an effort to learn it by heart given its great significance, a ruling echoed by Kitzur Shulchan Arukh Yalkut Yosef, Orach Chaim 6:3 and by Rav Mordechai Eliyahu (Maamar Mordechai Limot HaChol 3:17), who adds that one must be careful not to skip even a single word of the text.
Several later authorities address practical complications in timing and circumstance: Kitzur Shulchan Arukh Yalkut Yosef, Orach Chaim 7:11 discusses what to do when one forgot the blessing and needed to urinate again shortly after, offering two approaches to covering both obligations with blessings; Kitzur Shulchan Arukh Yalkut Yosef, Orach Chaim 7:8 rules that a person using a catheter must bless each time he senses the flow has stopped; and Shenei Luchot HaBerit, Shaar HaOtiyot, Tahara:17 discusses using a clean vessel at night for urination so that one may still bless Asher Yatzar and learn Torah afterward.
Kol Bo 23:32 and Drisha, Orach Chayim 7:2:1 continue the earlier Rishonic debate over whether washing the hands and its blessing are required before or alongside Asher Yatzar, particularly when one intends to proceed to Torah study, while Halakhot Ketanot, Part I 137:1-2 takes up the conceptual question of why a special blessing of this kind was instituted for the act of relieving oneself at all.
Revavos Ephraim (Revavos Ephraim 2:14) addresses whether Asher Yatzar may be recited during Pesukei Dezimrah, citing lenient views that permit saying it immediately as well as a practice of delaying it until after prayer, while maintaining that the blessing itself should not be interrupted to answer Amen.
Tzitz Eliezer (Tzitz Eliezer 7:27) rules that after reciting Birkat HaMapil at night, one is still permitted and indeed obligated to recite Asher Yatzar if needed, since intervening speech of this kind is not considered a halachic interruption, and that it is likewise permissible in that context to add a personal formulation thanking Hashem for one's bodily openings.
Shevet HaLevi (Shevet HaLevi 3:94) rules that a hospitalized patient who urinates only through a catheter may still recite Asher Yatzar, reasoning that the blessing concerns the beneficial bodily function itself and that the catheter serves to prevent a dangerous blockage, extending this logic as well to a person who drips urine involuntarily.
Maayan Omer (Maayan Omer 1:10:45) rules that if someone mistakenly begins the text of Asher Yatzar when he had intended to recite Me'ein Shalosh, he must restart Me'ein Shalosh even if he catches the error within the time it takes to speak, since the two blessings address entirely different matters and the mistaken opening invalidates what was said.