By Algebraic Geometry I from Görtz, Wedhorn page 60 $f^\sharp_x$ is the unique ring homomorphism which makes the diagram $A\to B \to B_{p_x}$, $A\to A_{p_{f(x)}}\to B_{p_x}$ commutative. The first arrow is $\varphi$ and the last $f^\sharp_x$. The two other arrows are the canonical homomorphisms. Can you please explain, why $f^\sharp_x$ makes the diagram commutative and why only $f^\sharp_x$ makes the diagram commutative?
Background: $f:Spec B \to Spec A$ is a morphism of affine schemes, $\varphi:=\Gamma(f):=f^\flat_{Spec A}$ and $p_x$ is the prime ideal of $B$ corresponding to a point $X\in Spec B$.
For any morphism of locally ringed spaces $f:X\to Y$, if $x\in X$, $y=f(x)$, the "stalk map" $f_x^\sharp:\mathscr{O}_{Y,y}\to\mathscr{O}_{X,x}$ is (by definition!) the unique homomorphism such that, for any open subset $V\subseteq Y$ containing $y$, the composite $\mathscr{O}_Y(V)\to\mathscr{O}_X(f^{-1}(V))\to\mathscr{O}_{X,x}$ is equal to $\mathscr{O}_Y(V)\to\mathscr{O}_{Y,y}\xrightarrow{f_x^\sharp}\mathscr{O}_{X,x}$ (the uniqueness and existence follow from the universal property of colimits, and you should check this if you don't understand).
Now let's specialize to the case $X=\mathrm{Spec}(B)$ and $Y=\mathrm{Spec}(A)$, and $f:X\to Y$ is the morphism corresponding to a ring map $\varphi:A\to B$. Then $x$ is a prime $\mathfrak{q}\in\mathrm{Spec}(B)$, $y$ is a prime $\mathfrak{p}\in\mathrm{Spec}(A)$, and $\varphi^{-1}(\mathfrak{q})=\mathfrak{p}$. By the definition of the structure sheaf of the spectrum of a ring, $\mathscr{O}_{Y,y}=A_\mathfrak{p}$ and $\mathscr{O}_{X,x}=B_\mathfrak{q}$ (these equalities are really canonical isomorphisms of $A$ and $B$-algebras, respectively), and these identifications are such that $\mathscr{O}_X(X)=B\to\mathscr{O}_{X,x}=B_\mathfrak{q}$ is the localization map, and likewise for $\mathscr{O}_Y(Y)=A\to\mathscr{O}_{Y,y}=A_\mathfrak{p}$. As a specific instance of what I've said above about the map $f_x^\sharp$, with $V=Y$, so $f^{-1}(V)=X$, we have the the map $\varphi=f^\sharp_Y:\mathscr{O}_Y(Y)=A\to\mathscr{O}_X(X)=B\to\mathscr{O}_{X,x}=B_\mathfrak{q}$ is the same as the map $A\to A_\mathfrak{p}\xrightarrow{f_x^\sharp}B_\mathfrak{q}$. So $f_x^\sharp$ makes the diagram (that I haven't written down, but have just expressed as an equality of composites of ring maps) commutative by the definition of $f_x^\sharp$ (that I explained above, for any map of LRS) and by the identifications coming from the definition of the structure sheaf on the spectrum of a ring. The fact that there is only one ring map $A_\mathfrak{p}\to B_\mathfrak{q}$ making the (implicit) diagram commute is a consequence of the universal property of localization, which ensures that $A\to A_\mathfrak{p}$ is an epimorphism in the category of rings. Indeed, if $\psi:A_\mathfrak{p}\to B_\mathfrak{q}$ were another map making the (implicit) diagram commute, then pre-composing with $A\to A_\mathfrak{p}$ and using the commutativity, we get that
$$A\to A_\mathfrak{p}\xrightarrow{f_x^\sharp}B_\mathfrak{q}=A\to A_\mathfrak{p}\xrightarrow{\psi}B_\mathfrak{q}$$
so, since $A\to A_\mathfrak{p}$ is a ring epimorphism, $\psi=f_x^\sharp$.
EDIT: This is a response to the questions asked by the OP in the comments. First, the use that localizations are epimorphisms to show that $A_\mathfrak{p}\to B_\mathfrak{q}$ is the unique $A$-algebra map between these two rings, where the target is an $A$-algebra via $A\to B\to B_\mathfrak{q}$, is not superfluous. I'm not talking about maps of locally ringed space here, just maps of rings. If I had said that, for each $f\in A$, $f\notin\mathfrak{p}$, $A_f\to A_\mathfrak{p}\to B_\mathfrak{q}$ is the same as $A_f\to B_{\varphi(f)}\to B_\mathfrak{q}$, then I'd be using the universal property of colimits (because $A_\mathfrak{p}=\mathrm{colim}_{f\notin\mathfrak{p}}A_f$, but I didn't). The condition characterizing $f_x^\sharp$ for a general map of locally ringed spaces is coming from the universal property of colimits, but in the special case of rings, you only have to consider the open set $V=Y$ in the discussion above (because of the universal property of localization).
I guess your second question is why $f(x)=\varphi^{-1}(\mathfrak{q})$. So now you're working with an arbitrary morphism of LRS $f:\mathrm{Spec}(B)\to\mathrm{Spec}(A)$ and you want to prove that $f(x)=\varphi^{-1}(\mathfrak{q})$ if $x=\mathfrak{q}\in\mathrm{Spec}(B)$, i.e., the map of topological spaces is determined by the map on global sections for morphisms of spectra of rings. For this, see my answer here for a complete explanation of the result you're interested in (I don't want to rewrite the whole thing here).
EDIT: I guess I'll elaborate a bit. We have $f(\mathfrak{q})=\mathfrak{p}$ and we want to prove that $\varphi^{-1}(\mathfrak{q})=\mathfrak{p}$. We know what the map $f_x^\sharp:A_\mathfrak{p}\to B_\mathfrak{q}$ is, and we know that it is local because we're working with maps of local rings. We also know that this map $f_x^\sharp$ is compatible with the respective localization maps $A\to A_\mathfrak{p}$ and $B\to B_\mathfrak{q}$ (there's a commutative diagram here that I don't know how to make on MSE). My argument at the aforementioned link shows that $f(\mathfrak{q})$ is the inverse image in $A$ of $\mathfrak{q}B_\mathfrak{q}$ under $A\to B\to B_\mathfrak{q}$, which is $\varphi^{-1}(\mathfrak{q})$ (the inverse image of $\mathfrak{q}B_\mathfrak{q}$ in $B$ is $\mathfrak{q}$). Now if we pull back $\mathfrak{q}B_\mathfrak{q}$ to $A$ the other way around the diagram, which we already know by commutativity of the diagram gives us $f(\mathfrak{q})$, we get $\mathfrak{p}A_\mathfrak{p}$ in $A_\mathfrak{p}$ (since the map is local), and then it is a standard property of localization that the inverse image of the maximal ideal of $A_\mathfrak{p}$ in $A$ is $\mathfrak{p}$. So $\varphi^{-1}(\mathfrak{q})=f(\mathfrak{q})=\mathfrak{p}$, and we win.