[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]