The Gemara (Berachot 62a) relates that Rabbi Ami and Rabbi Assi would go out to the restroom alone, despite the fact that in those days, restrooms were out in the open fields, where one was exposed to harmful spirits. Rabbi Ami and Rabbi Assi explained that by remaining silent and conducting themselves appropriately in the restroom, they ensured their protection from harmful spirits.
Later (62b), the Gemara tells that Rabbi Abba made a noise outside to restroom to see if anyone was inside before he entered. Rav Safra was in the restroom, and responded, "Come in." Afterward, Rabbi Abba reprimanded Rav Safra for speaking while in the restroom, as he should have instead made a sound to indicate that the restroom was occupied. (It appears that Rav Safra spoke because he did not want Rabbi Abba to delay using the restroom for even a moment.)
The Or Zarua (Rav Yishak of Vienna, 1200-1270), based on these stories, writes that speaking while using the restroom is inappropriate. This is cited by the Rama, in his glosses to the Shulhan Aruch (Orah Haim 3:1). The Rambam (Hilchot De'ot 5:6) similarly included refraining from speaking while using the restroom in his list of behaviors that Torah scholars must practice in the interest of Seniut (discretion and propriety). The Lehem Mishnah commentary (Rav Abraham di Boton, d. 1588) clarifies that although the Rambam mentioned this as a practice observed by Torah scholars, it is proper for all people to adopt this practice.
Whereas these sources speak of refraining from speech as required for the purpose of Seniut, the Sefer Haredim (Rav Elazar Azikri, Sefat, 1533-1600) writes that this is required to avoid the danger posed by the harmful spirits that lurk in the restroom, as indicated by one of the aforementioned stories told in the Gemara.
Hacham Ovadia Yosef was asked about a situation where one is expecting an urgent phone call, and receives a call while using the restroom. Is it permissible to answer the phone in such a case?
Hacham Ovadia replied that if a considerable financial loss is potentially at stake, or if there is some other urgent matter, then there is a basis for allowing one to answer the call while using the restroom, though he should speak only the minimum amount necessary. This leniency is based on the fact that the concern for harmful spirits likely does not pertain to modern-day restrooms. Rav Yaakob Emden (Germany, 1697-1776) writes that harmful spirits lurked in the restrooms in the times when the restrooms were located out in the fields, but not in our times, when restrooms are located in the city. (Rav Yaakob Emden explained on this basis why we are not accustomed to reciting "Kabedu Mechubadim" – the declaration that a person would make before entering the restroom asking the angels that accompanied him to wait outside the restroom.) Secondly, the Maharshal (Rav Shlomo Luria, Poland, 1510-1573) wrote that Tum'a (impurity) no longer descends upon the hands as it did in ancient times. According to this opinion, we may assume that there is no longer spiritual impurity in the restroom, for the Ben Ish Hai (Rav Yosef Haim of Baghdad, 1833-1909) writes that the impurity on the hands is more harmful than the impurity in the restroom. Hence, if we no longer need to be concerned about the impurity on the hands, we certainly do not need to worry about the impurity of the restroom. Thirdly, because of modern plumbing, the bodily waste is immediately expelled from the restroom, whereas in ancient times it remained there in the restroom, and it is thus possible that no harmful spirits lurk in modern-day bathrooms. Therefore, speaking in the restroom does not pose any danger.
As mentioned, speaking in the restroom is forbidden also for a second reason – that this constitutes a breach of Seniut. Hacham Ovadia writes that nevertheless, when a considerable financial loss is at stake, we may allow one to speak, as long as he keeps the conversation as brief as possible.
The Yalkut Yosef writes that it is permissible to speak in the restroom about matters related to using the restroom – such as if one needs to ask somebody to bring him more toilet paper, or to bring him water for cleaning himself.
Summary: It is inappropriate to speak when in the restroom. If somebody is expecting an urgent phone call, and the phone rings when he is using the restroom, he may answer if a considerable financial loss is at stake, but he must speak as briefly as possible. It is permissible while using the restroom to ask somebody to bring more toilet paper or water that one needs for cleaning himself.

