[proofplan]
Exactness identifies $U$ with $\operatorname{im}(f)=\ker(g)$ and identifies $W$ with the quotient information carried by $g$. The main argument is a basis-lifting calculation: a basis of $\ker(g)$, together with chosen lifts of a basis of $W$, forms a basis of $V$. This proves the dimension formula directly when $U$ and $W$ are finite-dimensional, and the remaining cases follow by applying the same calculation to bases extracted from the finite-dimensional ambient space $V$.
[/proofplan]
[step:Translate exactness into the two structural facts used below]
Since the sequence is exact, the map $f:U \to V$ is injective, the map $g:V \to W$ is surjective, and
\begin{align*}
\operatorname{im}(f)=\ker(g).
\end{align*}
Because $f$ is injective, the map
\begin{align*}
f:U &\to \operatorname{im}(f)
\end{align*}
is an isomorphism of $k$-vector spaces. Hence $U$ is finite-dimensional if and only if $\operatorname{im}(f)$ is finite-dimensional, and in that case
\begin{align*}
\dim_k U=\dim_k \operatorname{im}(f)=\dim_k \ker(g).
\end{align*}
[/step]
[step:Prove the basis-lifting dimension calculation]
Let $K:=\ker(g)$, viewed as a $k$-vector subspace of $V$. Suppose first that $K$ and $W$ are finite-dimensional. Choose a basis
\begin{align*}
(a_1,\dots,a_r)
\end{align*}
of $K$, where $r=\dim_k K$. Choose a basis
\begin{align*}
(c_1,\dots,c_s)
\end{align*}
of $W$, where $s=\dim_k W$. Since $g:V \to W$ is surjective, for each index $j \in \{1,\dots,s\}$ choose an element $b_j \in V$ such that
\begin{align*}
g(b_j)=c_j.
\end{align*}
We claim that
\begin{align*}
(a_1,\dots,a_r,b_1,\dots,b_s)
\end{align*}
is a basis of $V$.
To prove spanning, let $v \in V$. Since $(c_1,\dots,c_s)$ spans $W$, there are scalars $\beta_1,\dots,\beta_s \in k$ such that
\begin{align*}
g(v)=\sum_{j=1}^{s}\beta_j c_j.
\end{align*}
Define
\begin{align*}
v_0:=v-\sum_{j=1}^{s}\beta_j b_j \in V.
\end{align*}
By linearity of $g$ and the choice of the $b_j$,
\begin{align*}
g(v_0)=g(v)-\sum_{j=1}^{s}\beta_j g(b_j)=g(v)-\sum_{j=1}^{s}\beta_j c_j=0.
\end{align*}
Thus $v_0 \in K$. Since $(a_1,\dots,a_r)$ spans $K$, there are scalars $\alpha_1,\dots,\alpha_r \in k$ such that
\begin{align*}
v_0=\sum_{i=1}^{r}\alpha_i a_i.
\end{align*}
Therefore
\begin{align*}
v=\sum_{i=1}^{r}\alpha_i a_i+\sum_{j=1}^{s}\beta_j b_j,
\end{align*}
so the displayed list spans $V$.
To prove linear independence, suppose scalars $\alpha_1,\dots,\alpha_r,\beta_1,\dots,\beta_s \in k$ satisfy
\begin{align*}
\sum_{i=1}^{r}\alpha_i a_i+\sum_{j=1}^{s}\beta_j b_j=0.
\end{align*}
Applying the linear map $g$ gives
\begin{align*}
0=\sum_{i=1}^{r}\alpha_i g(a_i)+\sum_{j=1}^{s}\beta_j g(b_j).
\end{align*}
Each $a_i$ lies in $K=\ker(g)$, so $g(a_i)=0$, while $g(b_j)=c_j$. Hence
\begin{align*}
\sum_{j=1}^{s}\beta_j c_j=0.
\end{align*}
Since $(c_1,\dots,c_s)$ is linearly independent, every $\beta_j$ is $0$. The original relation becomes
\begin{align*}
\sum_{i=1}^{r}\alpha_i a_i=0.
\end{align*}
Since $(a_1,\dots,a_r)$ is linearly independent, every $\alpha_i$ is $0$. Thus the displayed list is linearly independent, and therefore it is a basis of $V$.
Consequently $V$ is finite-dimensional and
\begin{align*}
\dim_k V=r+s=\dim_k K+\dim_k W.
\end{align*}
[guided]
The point of this step is to show exactly how a short exact sequence decomposes a basis of the middle term into two parts: one part lying in the kernel of $g$, and one part mapping onto a basis of $W$.
Let $K:=\ker(g)$. Assume $K$ and $W$ are finite-dimensional. Choose a basis
\begin{align*}
(a_1,\dots,a_r)
\end{align*}
of $K$, where $r=\dim_k K$, and choose a basis
\begin{align*}
(c_1,\dots,c_s)
\end{align*}
of $W$, where $s=\dim_k W$. Since $g:V \to W$ is surjective, every basis vector $c_j$ has at least one preimage in $V$. For each $j \in \{1,\dots,s\}$, choose $b_j \in V$ satisfying
\begin{align*}
g(b_j)=c_j.
\end{align*}
We prove that
\begin{align*}
(a_1,\dots,a_r,b_1,\dots,b_s)
\end{align*}
is a basis of $V$. First, let $v \in V$. Since the $c_j$ span $W$, there are scalars $\beta_1,\dots,\beta_s \in k$ such that
\begin{align*}
g(v)=\sum_{j=1}^{s}\beta_j c_j.
\end{align*}
Subtract from $v$ the corresponding lifted combination:
\begin{align*}
v_0:=v-\sum_{j=1}^{s}\beta_j b_j.
\end{align*}
This subtraction is designed to kill the image under $g$. Indeed, by linearity of $g$,
\begin{align*}
g(v_0)=g(v)-\sum_{j=1}^{s}\beta_j g(b_j).
\end{align*}
Using $g(b_j)=c_j$, this becomes
\begin{align*}
g(v_0)=g(v)-\sum_{j=1}^{s}\beta_j c_j=0.
\end{align*}
Hence $v_0 \in \ker(g)=K$. Since the $a_i$ span $K$, there are scalars $\alpha_1,\dots,\alpha_r \in k$ with
\begin{align*}
v_0=\sum_{i=1}^{r}\alpha_i a_i.
\end{align*}
Substituting the definition of $v_0$ gives
\begin{align*}
v=\sum_{i=1}^{r}\alpha_i a_i+\sum_{j=1}^{s}\beta_j b_j.
\end{align*}
Thus the chosen vectors span $V$.
Now suppose there is a linear relation
\begin{align*}
\sum_{i=1}^{r}\alpha_i a_i+\sum_{j=1}^{s}\beta_j b_j=0.
\end{align*}
Apply $g$ to the relation. Since each $a_i$ lies in $K=\ker(g)$, we have $g(a_i)=0$. Since $g(b_j)=c_j$, the relation becomes
\begin{align*}
\sum_{j=1}^{s}\beta_j c_j=0.
\end{align*}
The vectors $c_1,\dots,c_s$ are linearly independent, so $\beta_j=0$ for every $j$. The original relation is then
\begin{align*}
\sum_{i=1}^{r}\alpha_i a_i=0.
\end{align*}
The vectors $a_1,\dots,a_r$ are linearly independent, so $\alpha_i=0$ for every $i$. Therefore all coefficients in the original relation vanish, proving linear independence.
We have shown that the list has $r+s$ vectors and is a basis of $V$. Hence $V$ is finite-dimensional and
\begin{align*}
\dim_k V=r+s=\dim_k K+\dim_k W.
\end{align*}
[/guided]
[/step]
[step:Handle the case where $U$ and $W$ are finite-dimensional]
Assume $U$ and $W$ are finite-dimensional. By the first step, $K=\ker(g)=\operatorname{im}(f)$ is finite-dimensional and
\begin{align*}
\dim_k K=\dim_k U.
\end{align*}
Applying the basis-lifting calculation to $K$ and $W$ shows that $V$ is finite-dimensional and
\begin{align*}
\dim_k V=\dim_k K+\dim_k W=\dim_k U+\dim_k W.
\end{align*}
[/step]
[step:Handle the cases where $V$ is finite-dimensional]
Assume $V$ is finite-dimensional. Every subspace of a finite-dimensional vector space is finite-dimensional, so $K=\ker(g)$ is finite-dimensional. Choose a basis $(a_1,\dots,a_r)$ of $K$ and extend it to a basis
\begin{align*}
(a_1,\dots,a_r,b_1,\dots,b_s)
\end{align*}
of $V$.
We now prove that $(g(b_1),\dots,g(b_s))$ is a basis of $W$. First, let $w \in W$. Since $g:V \to W$ is surjective, there exists $v \in V$ such that $g(v)=w$. Because $(a_1,\dots,a_r,b_1,\dots,b_s)$ is a basis of $V$, there are scalars $\alpha_1,\dots,\alpha_r,\beta_1,\dots,\beta_s \in k$ such that
\begin{align*}
v=\sum_{i=1}^{r}\alpha_i a_i+\sum_{j=1}^{s}\beta_j b_j.
\end{align*}
Applying $g$ and using $g(a_i)=0$ for every $i$ gives
\begin{align*}
w=g(v)=\sum_{j=1}^{s}\beta_j g(b_j).
\end{align*}
Thus the vectors $g(b_1),\dots,g(b_s)$ span $W$.
To prove linear independence, suppose scalars $\beta_1,\dots,\beta_s \in k$ satisfy
\begin{align*}
\sum_{j=1}^{s}\beta_j g(b_j)=0.
\end{align*}
Then
\begin{align*}
g\Bigl(\sum_{j=1}^{s}\beta_j b_j\Bigr)=0,
\end{align*}
so $\sum_{j=1}^{s}\beta_j b_j \in K$. Since $(a_1,\dots,a_r,b_1,\dots,b_s)$ is a basis of $V$ and $(a_1,\dots,a_r)$ is a basis of $K$, the vector $\sum_{j=1}^{s}\beta_j b_j$ has zero coordinates along the $b_j$-directions only when every $\beta_j$ is $0$. Therefore $g(b_1),\dots,g(b_s)$ are linearly independent. Hence they form a basis of $W$, so $W$ is finite-dimensional and
\begin{align*}
\dim_k W=s.
\end{align*}
Also $\dim_k K=r$, and since $f:U \to K$ is an isomorphism, $\dim_k U=\dim_k K=r$. Therefore, whenever $V$ is finite-dimensional and either $U$ or $W$ is already known to be finite-dimensional, the remaining space is finite-dimensional and
\begin{align*}
\dim_k V=r+s=\dim_k U+\dim_k W.
\end{align*}
In particular, this covers both cases where $U$ and $V$ are finite-dimensional and where $V$ and $W$ are finite-dimensional.
[/step]
[step:Conclude the dimension identity in all allowed cases]
The hypothesis says that at least two of $U$, $V$, and $W$ are finite-dimensional. If the finite-dimensional spaces are $U$ and $W$, the third step proves that $V$ is finite-dimensional and gives the desired identity. If $V$ is one of the finite-dimensional spaces, the preceding step proves that the remaining space is finite-dimensional and gives the same identity. Hence in every case covered by the hypothesis,
\begin{align*}
\dim_k V=\dim_k U+\dim_k W.
\end{align*}
This proves the theorem.
[/step]