Let $K_j(A,b)$ denote the Krylov subspace $$K_j(A,b) = \text{span} \{b,Ab,\dotsc, A^{j-1} b\}. $$By definition, the GMRES (Generalized Minimal RESidual) algorithm computes $x_k \in K_k(A,b)$ such that $$\|b-Ax_k\|_2 = \min \{ \|b - Ax \|_2 \: : \: x \in K_k(A,b)\}.$$
We say, that GMRES minimizes the residual $b-Ax$ over all $x \in K_k(A,b)$. Now, if $x \in K_k(A,b)$ then $x = p(A)b$ for at least one polynomial $p$ of degree strictly less than $k$. The corresponding residual is $$r = b - Ax = b - Ap(A)b = q(A)b,$$ where $$q(t) = 1 - tp(t)$$ has $q(0) = 1$ and the degree of $q$ is at most $k$. Conversely, if $q$ is a polynomial of degree at most $k$ and $q(0) = 1$, then $x=0$ is a root of $q(x) - 1$ and we can therefore factor $q(x) - 1$ as $q(x) - 1 = x p(x)$ where $p$ is a polynomial of degree strictly less than $k$.
We conclude that finding the GMRES approximation $x_k$ is equivalent to minimizing $$\|q(A)b\|_2$$ where $q$ is a polynomial of degree at most $k$ and $q(0)=1$.