It doesn’t really matter what the domain and codomain of $f$ are: the result is true in general. You can prove it in the usual way: show that any element of the lefthand side is an element of the righthand side, and vice versa. I’ll do one direction and leave the other to you.
Suppose that $x\in f^{-1}\left[\bigcup_{i\in\mathcal{I}}Y_i\right]$; then $f(x)\in\bigcup_{i\in\mathcal{I}}Y_i$. By the definition of union there is some $i_0\in\mathcal{I}$ such that $f(x)\in Y_{i_0}$. But then $x\in f^{-1}[Y_{i_0}]\subseteq\bigcup_{i\in\mathcal{I}}f^{-1}[Y_i]$, and since $x\in f^{-1}\left[\bigcup_{i\in\mathcal{I}}Y_i\right]$ was arbitrary, it follows that $f^{-1}\left[\bigcup_{i\in\mathcal{I}}Y_i\right]\subseteq\bigcup_{i\in\mathcal{I}}f^{-1}[Y_i]\;.$