[proofplan]
We exploit the invariance $A(W) \subseteq W$ to produce a block upper-triangular matrix representation of $A$. Choose a basis of $W$ and extend it to a basis of $V$ using representatives for $V/W$. In this basis, the matrix of $A$ has the block form $\begin{pmatrix} [B] & * \\ 0 & [C] \end{pmatrix}$, where $[B]$ is the matrix of $A|_W$ in the chosen basis of $W$ and $[C]$ is the matrix of the induced quotient map $C: V/W \to V/W$ in the induced basis of $V/W$. The trace is basis-independent, and the trace of a block upper-triangular matrix is the sum of the traces of its diagonal blocks, giving $\operatorname{tr}(A) = \operatorname{tr}(B) + \operatorname{tr}(C)$.
[/proofplan]
[step:Choose a basis of $W$ and extend to a basis of $V$]
Since $V$ is finite-dimensional, so is $W$. Let $\dim W = k$ and $\dim V = k + \ell$, with $\ell = \dim(V/W) \geq 0$. Choose a basis $(w_1, \ldots, w_k)$ of $W$. By the basis extension theorem, extend this to a basis
\begin{align*}
\mathcal{B} = (w_1, \ldots, w_k, v_1, \ldots, v_\ell)
\end{align*}
of $V$.
Let $\pi: V \to V/W$ denote the canonical quotient map, $v \mapsto v + W$. The images $(\pi(v_1), \ldots, \pi(v_\ell))$ form a basis $\bar{\mathcal{B}}$ of $V/W$: they span because every element of $V/W$ is the image under $\pi$ of some $v \in V$, which can be written $v = \sum_i \alpha_i w_i + \sum_j \beta_j v_j$ with $\pi(v) = \sum_j \beta_j \pi(v_j)$ (the $\pi(w_i) = 0$ terms drop); and they are linearly independent because a relation $\sum_j \beta_j \pi(v_j) = 0$ in $V/W$ means $\sum_j \beta_j v_j \in W$, which combined with the linear independence of the full basis $\mathcal{B}$ forces each $\beta_j = 0$.
[/step]
[step:Write $A$ in block form with respect to the chosen basis]
We compute the matrix $[A]_\mathcal{B}$ of $A$ with respect to the basis $\mathcal{B}$.
**Action on $W$-basis vectors.** For each $i \in \{1, \ldots, k\}$, the hypothesis $A(W) \subseteq W$ gives $A(w_i) \in W$, so $A(w_i)$ is a linear combination of $w_1, \ldots, w_k$ alone:
\begin{align*}
A(w_i) = \sum_{i'=1}^{k} B_{i'i}\, w_{i'}.
\end{align*}
The coefficients $B_{i'i}$ form the $k \times k$ matrix $[B]_{(w_i)} = (B_{i'i})$ of $B = A|_W: W \to W$ in the basis $(w_1, \ldots, w_k)$.
**Action on complementary basis vectors.** For each $j \in \{1, \ldots, \ell\}$, the image $A(v_j) \in V$ expands in the full basis as
\begin{align*}
A(v_j) = \sum_{i=1}^{k} A'_{ij}\, w_i + \sum_{j'=1}^{\ell} C_{j'j}\, v_{j'}
\end{align*}
for some scalars $A'_{ij}, C_{j'j}$. Applying $\pi$ to both sides kills the $w_i$ terms; and since $A$ preserves $W$, the induced quotient map $C: V/W \to V/W$ is well-defined, $C(\pi(v)) = \pi(A(v))$, so
\begin{align*}
C(\pi(v_j)) = \pi(A(v_j)) = \sum_{j'=1}^{\ell} C_{j'j}\, \pi(v_{j'}).
\end{align*}
The coefficients $C_{j'j}$ are therefore the entries of the $\ell \times \ell$ matrix $[C]_{\bar{\mathcal{B}}} = (C_{j'j})$ of $C$ in the basis $\bar{\mathcal{B}}$ of $V/W$.
Collecting these formulas, the matrix of $A$ in the basis $\mathcal{B}$ has the block upper-triangular form
\begin{align*}
[A]_\mathcal{B} = \begin{pmatrix} [B]_{(w_i)} & A' \\ 0_{\ell \times k} & [C]_{\bar{\mathcal{B}}} \end{pmatrix},
\end{align*}
where $A' = (A'_{ij})$ is the $k \times \ell$ block coming from the $w$-components of $A(v_j)$, and the lower-left block is the $\ell \times k$ zero matrix (because no $A(w_i)$ has any $v_j$-component — this is the invariance $A(W) \subseteq W$).
[guided]
The whole content of this step is to translate the abstract invariance hypothesis $A(W) \subseteq W$ into the concrete matrix statement "the lower-left block is zero." Let us verify it explicitly.
The $(j, i)$ entry of the lower-left block would be the coefficient of $v_j$ in the expansion of $A(w_i)$ in the basis $\mathcal{B}$. But by hypothesis, $A(w_i) \in W$, and $W = \operatorname{span}(w_1, \ldots, w_k)$ in the basis $\mathcal{B}$ — none of the $v_j$ are in $W$. By uniqueness of the basis expansion, the coefficient of $v_j$ in $A(w_i)$ must be zero for every $j$, giving the zero lower-left block.
The upper-right block $A'$ need not be zero in general: $A(v_j)$ is an arbitrary element of $V$, and can have a nontrivial component in $W$. That is why we only get block upper-triangular form, not block diagonal form.
The well-definedness of the quotient map $C$ requires checking that if $\pi(v) = \pi(v')$ then $\pi(A(v)) = \pi(A(v'))$. This reduces to showing $v - v' \in W \implies A(v - v') \in W$, which is exactly the invariance hypothesis. So the hypothesis $A(W) \subseteq W$ plays two roles: it makes $C$ well-defined, and it forces the lower-left block to vanish.
[/guided]
[/step]
[step:Compute the trace of the block matrix]
The trace of a square matrix is the sum of its diagonal entries, and this is invariant under change of basis: if $P$ is any invertible matrix and $M' = P^{-1} M P$, then $\operatorname{tr}(M') = \operatorname{tr}(M)$ (by the cyclic property $\operatorname{tr}(XY) = \operatorname{tr}(YX)$ applied to $X = P^{-1}M$, $Y = P$). Hence $\operatorname{tr}(A) = \operatorname{tr}([A]_\mathcal{B})$, where the right-hand side is the matrix trace in any basis.
For the block upper-triangular matrix
\begin{align*}
[A]_\mathcal{B} = \begin{pmatrix} [B]_{(w_i)} & A' \\ 0 & [C]_{\bar{\mathcal{B}}} \end{pmatrix},
\end{align*}
the diagonal entries are: the diagonal entries of the $k \times k$ block $[B]_{(w_i)}$ (for the first $k$ rows) and the diagonal entries of the $\ell \times \ell$ block $[C]_{\bar{\mathcal{B}}}$ (for the last $\ell$ rows). The off-diagonal block $A'$ contributes no diagonal entries to the full matrix, and the zero lower-left block contributes none either. Therefore
\begin{align*}
\operatorname{tr}([A]_\mathcal{B}) = \operatorname{tr}([B]_{(w_i)}) + \operatorname{tr}([C]_{\bar{\mathcal{B}}}).
\end{align*}
Since $[B]_{(w_i)}$ is the matrix of $B$ in a basis of $W$ and $[C]_{\bar{\mathcal{B}}}$ is the matrix of $C$ in a basis of $V/W$, the basis-independence of trace gives $\operatorname{tr}([B]_{(w_i)}) = \operatorname{tr}(B)$ and $\operatorname{tr}([C]_{\bar{\mathcal{B}}}) = \operatorname{tr}(C)$.
[/step]
[step:Conclude $\operatorname{tr}(A) = \operatorname{tr}(B) + \operatorname{tr}(C)$]
Combining the previous steps,
\begin{align*}
\operatorname{tr}(A) = \operatorname{tr}([A]_\mathcal{B}) = \operatorname{tr}([B]_{(w_i)}) + \operatorname{tr}([C]_{\bar{\mathcal{B}}}) = \operatorname{tr}(B) + \operatorname{tr}(C),
\end{align*}
which is the identity claimed.
[/step]