[proofplan]
The argument is the standard antichain-counting argument for $\kappa$-c.c. forcing. A name for a function is controlled coordinate-by-coordinate by maximal antichains below a fixed condition, and the $\kappa$-chain condition makes each coordinate have fewer than $\kappa$ possible values in the ground model. For cofinalities, the union of all possible values has size below the old cofinality and is therefore bounded in the ordinal. For cardinals, the same union has size below the alleged target cardinal, so it cannot cover that cardinal.
[/proofplan]
[step:Control each coordinate of a forced function by a small ground-model set]
Work in $M$. Let $p \in \mathbb{P}$ be a condition, let $\mu$ and $\lambda$ be ordinals of $M$, and let $\dot{f} \in M$ be a $\mathbb{P}$-name such that
\begin{align*}
p \Vdash_{\mathbb{P}} \dot{f}: \mu \to \lambda.
\end{align*}
For each $\xi < \mu$, define the dense set below $p$
\begin{align*}
D_\xi := \{q \in \mathbb{P} : q \leq p \text{ and there is some } \alpha < \lambda \text{ such that } q \Vdash_{\mathbb{P}} \dot{f}(\xi)=\alpha\}.
\end{align*}
By the forcing definition of function values, $D_\xi$ is dense below $p$: if $r \leq p$, then some extension $q \leq r$ decides the ordinal value of $\dot{f}(\xi)$.
Using [Zorn's lemma](/theorems/1226) in $M$, choose a maximal antichain $B_\xi \subseteq D_\xi$ below $p$. This choice is made in $M$, since $M$ satisfies ZFC and the forcing relation for names in $M$ is definable over $M$. Since $B_\xi \in M$ is an antichain in $\mathbb{P}$ and $M$ satisfies that $\mathbb{P}$ has the $\kappa$-chain condition, $M$ satisfies
\begin{align*}
|B_\xi| < \kappa.
\end{align*}
For each $q \in B_\xi$, choose in $M$ an ordinal $\alpha_q < \lambda$ such that
\begin{align*}
q \Vdash_{\mathbb{P}} \dot{f}(\xi)=\alpha_q.
\end{align*}
Define
\begin{align*}
A_\xi := \{\alpha_q : q \in B_\xi\}.
\end{align*}
Then $A_\xi \in M$, $A_\xi \subseteq \lambda$, and $M$ satisfies $|A_\xi| < \kappa$.
Finally, $p$ forces $\dot{f}(\xi) \in A_\xi$. Indeed, if not, then some $r \leq p$ would force $\dot{f}(\xi) \notin A_\xi$. Since $B_\xi$ is maximal below $p$, there is some $q \in B_\xi$ compatible with $r$; choose $s \leq q,r$. But $q$ forces $\dot{f}(\xi)=\alpha_q \in A_\xi$, while $r$ forces $\dot{f}(\xi) \notin A_\xi$, contradicting compatibility.
[guided]
The purpose of this step is to replace a possibly new function in the forcing extension by old, ground-model approximations to all of its possible values.
Fix $p \in \mathbb{P}$, ordinals $\mu,\lambda \in M$, and a $\mathbb{P}$-name $\dot{f} \in M$ such that
\begin{align*}
p \Vdash_{\mathbb{P}} \dot{f}: \mu \to \lambda.
\end{align*}
For a fixed coordinate $\xi < \mu$, we want to know which values $\dot{f}(\xi)$ can take below $p$. Define
\begin{align*}
D_\xi := \{q \in \mathbb{P} : q \leq p \text{ and there is some } \alpha < \lambda \text{ such that } q \Vdash_{\mathbb{P}} \dot{f}(\xi)=\alpha\}.
\end{align*}
This set is dense below $p$. Namely, if $r \leq p$, then because $p$ forces $\dot{f}$ to be a function from $\mu$ to $\lambda$, the condition $r$ can be refined to a condition $q \leq r$ deciding the value of the name $\dot{f}(\xi)$ as some ordinal $\alpha < \lambda$.
Now choose, inside $M$, a maximal antichain $B_\xi \subseteq D_\xi$ below $p$. This is legitimate in $M$ because $M$ satisfies ZFC, and the set $D_\xi$ is definable in $M$ from $p$, $\dot{f}$, $\xi$, $\mu$, $\lambda$, and $\mathbb{P}$ by definability of the forcing relation. This is where the chain condition enters. Since $B_\xi$ is an antichain in $\mathbb{P}$ and $M$ satisfies that $\mathbb{P}$ is $\kappa$-c.c., $M$ satisfies
\begin{align*}
|B_\xi| < \kappa.
\end{align*}
Each $q \in B_\xi$ decides $\dot{f}(\xi)$, so choose in $M$ an ordinal $\alpha_q < \lambda$ with
\begin{align*}
q \Vdash_{\mathbb{P}} \dot{f}(\xi)=\alpha_q.
\end{align*}
Define the ground-model set of possible values at coordinate $\xi$ by
\begin{align*}
A_\xi := \{\alpha_q : q \in B_\xi\}.
\end{align*}
Then $A_\xi \in M$, $A_\xi \subseteq \lambda$, and $M$ satisfies $|A_\xi| < \kappa$, because $A_\xi$ is the image of $B_\xi$ under the map $q \mapsto \alpha_q$.
It remains to check that $A_\xi$ really captures all possible values below $p$. Suppose toward a contradiction that $p$ does not force $\dot{f}(\xi) \in A_\xi$. Then there is some $r \leq p$ such that
\begin{align*}
r \Vdash_{\mathbb{P}} \dot{f}(\xi) \notin A_\xi.
\end{align*}
Since $B_\xi$ is maximal among conditions below $p$, the condition $r$ is compatible with some $q \in B_\xi$. Choose $s \in \mathbb{P}$ with $s \leq r$ and $s \leq q$. The condition $q$ forces $\dot{f}(\xi)=\alpha_q$, and $\alpha_q \in A_\xi$ by definition. Hence $s$ forces $\dot{f}(\xi) \in A_\xi$. But $s \leq r$, so $s$ also forces $\dot{f}(\xi) \notin A_\xi$. This contradiction proves
\begin{align*}
p \Vdash_{\mathbb{P}} \dot{f}(\xi) \in A_\xi.
\end{align*}
[/guided]
[/step]
[step:Preserve cofinalities at least $\kappa$]
Let $\lambda \in M$ be an ordinal such that
\begin{align*}
\theta := \operatorname{cf}^{M}(\lambda) \geq \kappa.
\end{align*}
Since every cofinal map in $M$ remains a cofinal map in $M[G]$, we have
\begin{align*}
\operatorname{cf}^{M[G]}(\lambda) \leq \theta.
\end{align*}
It remains to prove that the cofinality cannot decrease.
Suppose toward a contradiction that some condition $p \in \mathbb{P}$ forces that $\dot{f}: \mu \to \lambda$ is cofinal in $\lambda$, where $\mu \in M$ is an ordinal with $\mu < \theta$. Applying the previous step to $p$, $\dot{f}$, $\mu$, and $\lambda$, for each $\xi < \mu$ choose $A_\xi \in M$ such that $A_\xi \subseteq \lambda$, $M \models |A_\xi| < \kappa$, and
\begin{align*}
p \Vdash_{\mathbb{P}} \dot{f}(\xi) \in A_\xi.
\end{align*}
Define
\begin{align*}
A := \bigcup_{\xi < \mu} A_\xi.
\end{align*}
Then $A \in M$ and $A \subseteq \lambda$. Since $\mu < \theta$, $\kappa \leq \theta$, and $\theta$ is a regular cardinal in $M$, $M$ satisfies
\begin{align*}
|A| < \theta.
\end{align*}
Because $\theta=\operatorname{cf}^{M}(\lambda)$, every subset of $\lambda$ of $M$-cardinality below $\theta$ is bounded in $\lambda$. Thus there is some $\beta < \lambda$ in $M$ such that
\begin{align*}
A \subseteq \beta.
\end{align*}
Therefore
\begin{align*}
p \Vdash_{\mathbb{P}} \text{for every } \xi < \mu,\ \dot{f}(\xi) < \beta,
\end{align*}
contradicting that $p$ forces $\dot{f}$ to be cofinal in $\lambda$. Hence no such $\mu < \theta$ exists, and
\begin{align*}
\operatorname{cf}^{M[G]}(\lambda) \geq \theta.
\end{align*}
Together with the opposite inequality, this gives
\begin{align*}
\operatorname{cf}^{M[G]}(\lambda)=\operatorname{cf}^{M}(\lambda).
\end{align*}
[/step]
[step:Preserve ground-model cardinals at least $\kappa$]
Let $\lambda \in M$ be a cardinal of $M$ with $\lambda \geq \kappa$. Suppose toward a contradiction that $\lambda$ is not a cardinal in $M[G]$. Then for some ordinal $\mu \in M$ with $\mu < \lambda$, there are a condition $p \in \mathbb{P}$ and a $\mathbb{P}$-name $\dot{f} \in M$ such that
\begin{align*}
p \Vdash_{\mathbb{P}} \dot{f}: \mu \to \lambda \text{ is surjective}.
\end{align*}
Apply the first step to $p$, $\dot{f}$, $\mu$, and $\lambda$. For each $\xi < \mu$, choose $A_\xi \in M$ such that $A_\xi \subseteq \lambda$, $M \models |A_\xi| < \kappa$, and
\begin{align*}
p \Vdash_{\mathbb{P}} \dot{f}(\xi) \in A_\xi.
\end{align*}
Define
\begin{align*}
A := \bigcup_{\xi < \mu} A_\xi.
\end{align*}
Then
\begin{align*}
p \Vdash_{\mathbb{P}} \text{for every } \xi < \mu,\ \dot{f}(\xi) \in A.
\end{align*}
We claim that $M$ satisfies $|A| < \lambda$. If $\lambda=\kappa$, then $\mu < \kappa$, every $A_\xi$ has size below $\kappa$, and the regularity of $\kappa$ gives
\begin{align*}
|A| < \kappa=\lambda.
\end{align*}
If $\lambda>\kappa$, then $|\mu|^{M}<\lambda$ and $\kappa<\lambda$. Since $\lambda$ is a cardinal in $M$, and since for infinite cardinals $\rho,\sigma<\lambda$ one has $\rho\cdot\sigma=\max(\rho,\sigma)<\lambda$, we obtain
\begin{align*}
|A| \leq |\mu|^{M}\cdot \kappa < \lambda.
\end{align*}
Thus in all cases $A$ is a proper subset of $\lambda$ in $M$.
Choose $\alpha \in \lambda \setminus A$. Since $p$ forces that every value of $\dot{f}$ lies in $A$, it follows that
\begin{align*}
p \Vdash_{\mathbb{P}} \text{there is no } \xi < \mu \text{ such that } \dot{f}(\xi)=\alpha,
\end{align*}
contradicting that $p$ forces $\dot{f}$ to be surjective onto $\lambda$. Therefore no such collapse is forced, and every ground-model cardinal $\lambda \geq \kappa$ remains a cardinal in $M[G]$.
[/step]