Please look at picture of the circuit.
The state in the second last step, is the state after the second $H$ gate. Now, we only have to measure, and we are interested in the probability of the two possible outcomes $0$ and $1$.
This is done via the formalism of the partial measurement. Let $(n+1)$-qubits be in state
$$
|0\rangle|\Psi_0\rangle_n + |1\rangle|\Psi_1\rangle_n,
$$
where $\langle \Psi_0 |\Psi_0\rangle + \langle \Psi_1|\Psi_1\rangle = 1$.
If we measure the first qubit, then the probability of outcome $0$ is given by
$$
P(0) = \langle\Psi_0|\Psi_0\rangle
$$
and, similarly for outcome $1$.
This is exactly what is happening here, with
$$
|\Psi_0\rangle = \frac{1}{2}(|\psi\rangle|\phi\rangle + |\phi\rangle|\psi\rangle).
$$
With this, the computation is simple.
\begin{align*}
P(0) &= \langle\Psi_0|\Psi_0\rangle, \\
&= \frac{1}{2}(\langle\psi|\langle\phi| + \langle\phi|\langle\psi|) \frac{1}{2}(|\psi\rangle|\phi\rangle + |\phi\rangle|\psi\rangle).
\end{align*}
Here, the product of the first and the third term is 1, the product of the second and fourth term is also 1, and the two cross terms have product $\langle \psi|\phi\rangle \langle \psi|\phi\rangle = |\langle \psi|\phi\rangle|^2$.
Putting this all together, we get
$$
P(0) = \frac{1}{4}(1 + 1 + 2 |\langle \psi|\phi\rangle|^2), \\
= \frac{1}{2} + \frac{1}{2} |\langle \psi|\phi\rangle|^2.
$$