Claim. Assume we are given decomposition $V=U\oplus W$ and a linear operator $A:V\to V$. Let $\mathbf{e}_U=\{e_1,\ldots,e_k\}$ be a basis of $U$ ($U=\mathrm{span}\{e_1,\ldots,e_k\}$), and $\mathbf{e}_W=\{e_{k+1},\ldots,e_n\}$ be a basis of $W$ ($W=\mathrm{span}\{e_{k+1},\ldots,e_n\}$). Then the following conditions are equivalent
1) $A(U)\subset U$, $A(W)\subset W$
2) matrix $[A]$ of operator $A$ in basis $\mathbf{e}_V=\{e_1,\ldots,e_n\}$ is block diagonal.
Proof. $\Longrightarrow$ Let $i\in\{1,\ldots,k\}$ then $e_i\in U$ and $A(e_i)\in A(U)\subset U$. Since $U=\mathrm{span}\{e_1,\ldots,e_k\}$, we have $A(e_i)=\sum_{j=1}^k a_{j,i} e_j$. This means that coordiante reperesentation of vector $A(e_i)$ in basis $\mathbf{e}_V$ is $ [A(e_i)]=\begin{pmatrix}a_{1,i}\\ a_{2,i}\\...\\a_{k,i}\\ 0\\ 0\\ \ldots\\ 0\end{pmatrix}\tag{1} $ Hence $n-k$ last coordinates of $A(e_i)$ in basis $\mathbf{e}_V$ are zeros.
Now let $i\in\{k+1,\ldots,n\}$ then $e_i\in W$ and $A(e_i)\in A(W)\subset W$. Since $W=\mathrm{span}\{e_{k+1},\ldots,e_n\}$, we have $A(e_i)=\sum_{j=k+1}^n a_{j,i} e_j$. This means that coordiante reperesentation of vector $A(e_i)$ in basis $\mathbf{e}_V$ is $ [A(e_i)]=\begin{pmatrix}\\ 0\\ \ldots\\ 0\\ 0\\a_{k+1,i}\\ a_{k+2,i}\\...\\a_{n,i}\end{pmatrix}\tag{2} $ Hence $k$ first coordinates of $A(e_i)$ in basis $\mathbf{e}_V$ are zeros.
Recall that matrix of operator in some basis is a collections of columns $[A(e_i)]$, so the matrix $[A]$ of operator $A$ is $ [A]= \left(\begin{array}{cccc|cccc} a_{1,1} & a_{1,2} & \ldots & a_{1,k} & 0 & 0 & \ldots & 0\\ a_{2,1} & a_{2,2} & \ldots & a_{2,k} & 0 & 0 & \ldots & 0\\ \ldots & \ldots & \ldots & \ldots & \ldots & \ldots & \ldots & \ldots\\ a_{k,1} & a_{k,2} & \ldots & a_{k,k} & 0 & 0 & \ldots & 0\\ \hline 0 & 0 & \ldots & 0 & a_{k+1,1} & a_{k+1,2} & \ldots & a_{k+1,n}\\ 0 & 0 & \ldots & 0 & a_{k+2,1} & a_{k+2,2} & \ldots & a_{k+2,n}\\ \ldots & \ldots & \ldots & \ldots & \ldots & \ldots & \ldots & \ldots\\ 0 & 0 & \ldots & 0 & a_{n,1} & a_{n,2} & \ldots & a_{n,n} \end{array}\right)\tag{3} $ This is exactly means that $[A]$ is block diagonal.
$(\Longleftarrow)$ Assume that matrix $[A]$ is block diagonal, i.e. of the form $(3)$.
Let $i\in\{1,\ldots,k\}$, then coordinates of $A(e_i)$ is of the form $(1)$. This equivalent to say that $A(e_i)=\sum_{j=1}^k a_{j,i} e_j\in\mathrm{span}\{e_1,\ldots,e_k\}=U$. Take arbitrary $x\in U$, then $x=\sum_{j=1}^k x_j e_j$, then $A(x)=\sum\limits_{j=1}^k x_j A(e_i)\in\mathrm{span}\{A(e_1),\ldots,A(e_k)\}\subset\mathrm{span}\{e_1,\ldots,e_k\}=U$. Since for all $x\in U$ we have $A(x)\in U$ we conclude $A(U)\subset U$.
Let $i\in\{k+1,\ldots,n\}$, then coordinates of $A(e_i)$ is of the form $(2)$. This equivalent to say that $A(e_i)=\sum_{j=k+1}^n a_{j,i} e_j\in\mathrm{span}\{e_{k+1},\ldots,e_n\}=W$. Take arbitrary $x\in W$, then $x=\sum_{j=k+1}^n x_j e_j$, then $A(x)=\sum\limits_{j=k+1}^n x_j A(e_i)\in\mathrm{span}\{A(e_{k+1}),\ldots,A(e_n)\}\subset\mathrm{span}\{e_{k+1},\ldots,e_n\}=W$. Since for all $x\in W$ we have $A(x)\in W$ we conclude $A(W)\subset W$.