Suppose I have a map $f:X \rightarrow K(G,1)$, $X$ connected. Given a cellular map $S^k \rightarrow X$, $k>1$, I can consider the complex $X'$ obtained by attaching that cell. I can ask two questions: Can I extend $f$ to $X'$ and, if so, is this unique up to a homotopy relative $X$?
We can construct $K(G,1)$ as a cell complex with 2-skeleton given by a wedge of circles, one for each element of $G$, with disks attached via all relations in $G$. As well, any connected CW complex is homotopy equivalent to one with a 1-skeleton a wedge of circles, simply by contracting a maximal tree in the 1-skeleton. We can again replace by a homotopy equivalent complex to ensure that the 2-disks are attached via sequences of clockwise and counterclockwise loops around the circles in the 1-skeleton. This follows from our knowledge of the fundamental group of the wedge of circles.
The point of this is to make obvious how to define a map into a $K(G,1)$ given a homomorphism $\pi_1(X) \rightarrow K(G,1)$. Suppose we have $X$ following the conditions we just set out. Van Kampen tells us that the fundamental group of $X$ has presentation $\langle e^1_i | \phi(e^2_j) \rangle$ where the $e^1$ are the 1-cells and the $\phi$ are the characteristic maps written as words corresponding to how they wrap around the 1-cells.
Now given a homomorphism $f:\langle e^1_i | \phi(e^2_j) \rangle \rightarrow K(G,1)$, we can define a map on the 2-skeleton of $X$ by sending the cell $e^1_i$ to $f(e^1_i)$ and the disk $e^2_j$ to the disk corresponding to the relation $\phi(e^2_j)$ because every 2-cell in $X$ corresponds to a relation in $\pi_1(X)$ that is preserved by $f$ since $f$ is a homomorphism.
Let us abuse notation and call this map $f:X^2 \rightarrow K(G,1)$, from the 2-skeleton to the $K(G,1)$. Now we can return to the question of the first paragraph. Pick a 3-cell attached via $\psi$, can we extend this map along this cell? Well we can extend $f$ over the 3-cell, if and only if, $f \circ \psi$ is nullhomotopic (this is exactly the universal property of the cone on a map). Well, the great part about $K(G,1)$ is that all the higher homotopy groups vanish. Hence, we have no obstruction to this extension. Similarly, we can extend cell by cell until we have extend $f$ to a map $f:X \rightarrow K(G,1)$ and cellular approximation tells us that since we haven't changed the 2-skeleton the map on fundamental groups are the same. This is surjectivity in your question.
Now the second question is could these extension have been homotopically different. Well, let's say I extend $f$ to $f': X \cup e^k \rightarrow K(G,1)$ and $f'':X \cup e'^k \rightarrow K(G,1)$. Having a homotopy relative $X$ between these two is exactly the information of a map $X \cup e^k \cup e'^k \cup e^{k+1} \rightarrow K(G,1)$, where the $e^{k+1}$ is attached along the sphere $e^k \cup e'^k$, such that it restricts to $f'$ and $f''$ on the respective subcomplexes.
But this is just a version of the previous question! Now we wish to extend a map along a $(k+1)$-cell, which is again possible since the homotopy groups of the codomain vanish above 1.
So we see that the extension $f$ we came up with is homotopically unique, regardless of the choices we made. This almost gives us injectivity. All that is left is to check that any map $h:X \rightarrow K(G,1)$ can be homotoped to a map on 2-skeleton that coincides with how we defined the continuous function associated to $f: \pi_1(X) \rightarrow G$ because when it is in this form it can arise from extending the map on the 2-skeleton cell by cell, which we showed was homotopically unique.
First homotope $h$ so it is a cellular map. Then on the 1-skeleton this follows from the fact that if a $1$-cell is mapped around k 1-cells of $K(G,1)$ via $g_1 ^\pm \dots g_n ^\pm$ where $\pm$ denotes which way we wrap around the cell, the map restricted to this circle can be homotoped so that it is wrapped via the word $(\pm g_1) \dots (\pm g_n)$, where $\pm$ in front denotes whether we should take the inverse of $g_n$ or not. This follows from the fact that $(-g)g$ is a relation in $G$ and hence we add a disk along this relation in $K(G,1)$.
Presumably one could also argue directly for the 2-cells, but this would probably be gross. Instead we can resuse the above idea about extending maps again! We have shown that it is possible to homotope the map from the 2-skeleton so that 1-cells get wrapped around collections of 1-cells via words in $G$.
Consider $X^2 \times I$ and its subcomplex given by $(X^2 \times I) ^2$. The homotopy we just described gives a map from this subcomplex to $K(G,1)$ restricting to $f$ on $X^2 \times \{0\}$ and $g$ on $X^2 \times \{1\}$. Can we extend this map to all of $X^2 \times I$? Well the only thing left to add are 3-cells, and we both know that extending to 3-cells isn't an issue when the codomain is $K(G,1)$.
Hence, we have a homotopy from $g$ to $f$, on the 2-skeleton. By homotopy extension, we have a homotopy from $g$ defined on the whole $X$ to something that agrees with $f$ on the 2-skeleton. And now the homotopical uniqueness result from earlier implies injectivity.