[guided]The goal is to turn a path in $A$ from $a$ to $b$ into a path in $B$ from $f(a)$ to $f(b)$. Since the only data relating $a$ and $b$ is the identity proof $p : \operatorname{Id}_A(a,b)$, the appropriate tool is identity elimination, or path induction [citetheorem:9633].
We use the motive
\begin{align*}
C(x,q) \coloneqq \operatorname{Id}_B(f(a),f(x)),
\end{align*}
where $x : A$ and $q : \operatorname{Id}_A(a,x)$. This motive asks: if $x$ is connected to $a$ by an identity proof, can we produce an identity proof between $f(a)$ and $f(x)$?
Identity elimination reduces this question to the reflexive case. When $x$ is $a$ and $q$ is $\operatorname{refl}_a$, the target type becomes
\begin{align*}
C(a,\operatorname{refl}_a) \equiv \operatorname{Id}_B(f(a),f(a)).
\end{align*}
This type is inhabited by reflexivity, so define
\begin{align*}
d \coloneqq \operatorname{refl}_{f(a)}.
\end{align*}
Thus $d : C(a,\operatorname{refl}_a)$.
Now the path induction principle [citetheorem:9633] applied to $C$ and $d$ gives, for every $b : A$ and $p : \operatorname{Id}_A(a,b)$, a term
\begin{align*}
\operatorname{pathind}(d,a,b,p) : C(b,p).
\end{align*}
By the definition of $C$, this is precisely a term of
\begin{align*}
\operatorname{Id}_B(f(a),f(b)).
\end{align*}
We therefore define the action of $f$ on the identity proof $p$ by
\begin{align*}
\operatorname{ap}_f(p) \coloneqq \operatorname{pathind}(d,a,b,p).
\end{align*}
This gives the required term
\begin{align*}
\operatorname{ap}_f(p) : \operatorname{Id}_B(f(a),f(b)).
\end{align*}[/guided]