The Gemara in Masechet Sanhedrin (p. 101) states that it prohibited to take off Tefilin in front of one’s rabbi. Rashi explains that doing so is disrespectful, since removing the Tefilin Shel Rosh requires removing one’s hat. Based on this Gemara, the Shulhan Aruch (Siman 25) derives that it also prohibited for the same reason to remove Tefilin in front of a Sefer Torah.
According to most Poskim, there would be no problem removing the Tefilin Shel Yad from one’s arm in front of a rabbi or Torah, since that does not involve revealing a bare head. However, Aruch Hashulhan (Rav Yechiel Michel Epstein of Nevarduk, 1829-1908) rules that even removing the Shel Yad is disrespectful, if one exposes his upper arm. If he takes the Tefilin off from under his sleeve, he agrees that there is no problem.
The Sefer Shevet Hakehati, as well as the Rivivot Ephraim (HaRav Ephraim Greenblatt, Memphis TN-Yerushalayim, 1932-2014) discuss whether it is permitted to put on Tefilin in front of the rabbi or Torah. They conclude that it is permitted, even though donning Tefilin also entails exposing a bare head. They argue that the very act of removing Tefilin is an expression of lessening Ol Malchut Shamayim (the yoke of Heaven) upon oneself, which is problematic in front of the Rav, whereas putting on Tefilin, is an acceptance of Ol Malchut Shamayim, which cannot be taken as a sign of disrespect.
However, this Halacha does not apply nowadays for two reasons. First, the Hida (Rav Haim Yosef David Azulai, 1724-1807) writes in Birkeh Yosef that it ancient times, people wore full head coverings that made it impossible to remove Tefilin without exposing a bare head. Nowadays that we wear Kippot, it is possible to remove the Tefilin without taking off the Kippah. The second reason is brought by Rabbenu Yosef Haim of Baghdad (1833-1909) in his Od Yosef Hai. He argues that the prohibition of taking off Tefilin in front of the Torah was only in the times where the Torah was exposed without a case. Now that the Sepharadic Sefer Torah has a case, it is not disrespectful to remove the Tefilin, even if one exposes his head, as long as the case is closed.
Even if this specific Halacha no longer applies, it underscores the paramount importance of showing respect for the Sefer Torah. If the Halacha was so concerned with exposing a bear head for a short moment, how much more so talking in the presence of the Sefer Torah and other forms of disrespect must be avoided.
SUMMARY
One may take off Tefilin in front of the rabbi or Sefer Torah if he does not expose his head, or the Sefer Torah is in its closed case.