Immerse an affine schemes into $A^n_S$ Suppose $f: X\rightarrow S$ is of finite type, S is Noetherian. Now X=Spec B is affine, but the morphism f is not an affine morphism. S is not affine (or really f does not factor through any affine subscheme on S), nor is S of finite type over $\mathbb Z$. Now my question is, can we have an immersion of $X\rightarrow \mathbb{A}^n_S$ as S schemes? 
Remark: If S is affine this is trivial. Moreover when f is an affine morphism I think it can also be done. When S is of finite type over $\mathbb Z$ by transitivity of finite type we get a map $X\rightarrow \mathbb{A}^n_{\mathbb Z}$ therefore by universal property of fiber product we get a map $X\rightarrow \mathbb{A}^n_S$.  
 A: 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. 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$ is
quasi-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 covered
by 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 into
a closed subscheme because $f$ is quasi-compact). 
A: If by "immersion" you mean closed immersion (and I think you do), then the answer is no, unless the morphism $X\to S$ is affine: a closed immersion is affine, and $\mathbb{A}^n_S\to S$ is also affine, so if your immersion exists, then the composite $f:X\to S$ is affine.
On the other hand if $f$ is affine, then $f_*\mathcal{O}_X$ has a surjective morphism from some $\mathcal{O}_S[x_1,\dots,x_n]$ (as sheaves of $\mathcal{O}_S$-algebras), and the induced map between the relative spectra gives your closed immersion.
(notice that the fact that $X$ is an affine scheme is irrelevant)
