3 Handle non-separated case

[EDIT] I just remembered that if $S$ is separated and $X$ is affine, then the above reasoning implies that any morphism $X\to S$ is affine. I should say that I learned this fact from V. Berkovich.

[EDIT2] The case when $S$ is not necessarily separated.

Let $S$ be a quasi-compact scheme and let $f : X\to S$ be a morphism of finite type from an affine scheme $X=\mathrm{Spec}(B)$ to $S$. Then there exists an immersion of $S$-schemes $X\to \mathbb A^n_S$.

Proof: First note that $f$ is separated because $X$ is separated, and $f$ isquasi-compact by hypothesis. So $f_*O_X$ is quasi-coherent on $S$. We are going to construct a morphism of quasi-coherent algebras$O_S[x_1,\dots, x_n]\to f_*O_X$ which will induce an immersion$X\to {\mathrm{Spec}}(f_*O_X)\to \mathbb A^n_S$.

Let $V$ be an affine open subset of $S$. Then $X_V$ can be coveredby a finite number of principal open subsets $D_X(b_1),\dots, D_X(b_m)$of $X$ and there exist $c_1,\dots, c_r\in B$ such that $B_{b_i}=O(V)[c_1,\dots, c_r, b_i^{-1}]$ for all $i\le m$. By taking a finite affine covering of $S$, we find $a_1,\dots, a_n\in B$such that for any open subset $V$ of this covering, $X_V$ is covered by some $D_X(a_i)$'s and each of these $O(D_X(a_i))$ is generated over $O(V)$ by the $a_j$'s and $a_i^{-1}$.

Use $a_1, \dots, a_n$ to construct a morphism $O_{S}[x_1,\dots, x_n]\to f_*O_X$sending $x_i$ to $a_i$. Let us show that the induced morphism $\tau : X\to \mathbb A^n_S$ is an immersion. It is enough to check this over an affine open subset $V$ in the above covering of $S$. So consider $\tau_V : X_V\to \mathbb A^n_V$. Suppose that $X_V$ is covered by $a_1,\dots, a_m$. Then $$\tau_V(X_V)\subseteq \cup_{1\le i \le m} D_{\mathbb A^n_V}(x_i).$$
Moreover, $\tau_V^{-1}(D_{\mathbb A^n_V}(x_i))=X_V\cap D_X(a_i)=D_X(a_i)$ and$O(D_{\mathbb A^n_V}(x_i))\to D_X(a_i)$ is surjective by construction. So $\tau_V$ is a closed immersion into $\cup_{1\le i \le m} D_{\mathbb A^n_V}(x_i)$ and $\tau$ is an immersion (closed immersion into an open subscheme and also an open immersion intoa closed subscheme because $f$ is quasi-compact).

2 added 145 characters in body

As noticed by Mattia, you have to suppose $f$ affine. Now under the further assumption that $S$ is separated and $X$ is affine, there exists a closed immersion as you want (Mattia's arguments then work): for any affine open subset $V$ of $S$, the canonical morphism $$f^{-1}(V)=X\times_S V\to X\times_{\mathbb Z} V$$ is a closed immersion because $S$ is separated. As $X$ is also affine, we get a surjective map $O(X)\otimes_{\mathbb Z} O(V)\to O(f^{-1}(V))$. Choose sufficiently many elements $a_1,\dots, a_n\in O(X)$ so they generate $O(f^{-1}(V_i))$ over $O(V_i)$ for a finite affine covering of $S$. Then the natural map $O_S[x_1,\dots, x_n]\to f_*O_X$ sending $x_i$ to $a_i$ is surjective and induces a closed immersion of $X$ in $\mathbb A^n_S$.

[EDIT] I just remembered that if $S$ is separated and $X$ is affine, then the above reasoning implies that any morphism $X\to S$ is affine.

1

As noticed by Mattia, you have to suppose $f$ affine. Now under the further assumption that $S$ is separated and $X$ is affine, there exists a closed immersion as you want (Mattia's arguments then work): for any affine open subset $V$ of $S$, the canonical morphism $$f^{-1}(V)=X\times_S V\to X\times_{\mathbb Z} V$$ is a closed immersion because $S$ is separated. As $X$ is also affine, we get a surjective map $O(X)\otimes_{\mathbb Z} O(V)\to O(f^{-1}(V))$. Choose sufficiently many elements $a_1,\dots, a_n\in O(X)$ so they generate $O(f^{-1}(V_i))$ over $O(V_i)$ for a finite affine covering of $S$. Then the natural map $O_S[x_1,\dots, x_n]\to f_*O_X$ sending $x_i$ to $a_i$ is surjective and induces a closed immersion of $X$ in $\mathbb A^n_S$.