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$. 



Immerse an affine schemes into $A^n_S$
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). 


