I am making a function $f$ from a set of $3$ elements, say the set $A=\{a,b,c\}$, to a set $B$ of $5$ elements.
I can choose the value of $f(a)$ in $5$ ways. For every way of deciding what $f(a)$ is, there are $5$ ways to decide what $f(b)$ is, for a total of $5^2$ ways of deciding the values of $f(a)$ and $f(b)$. And for every way of doing that, there are $5$ choices for the value of $f(c)$, for a total of $5^3$.
If we want $f$ to be one to one, then for every choice about the value of $f(a)$, we only have $4$ choices for $f(b)$, since we cannot have $f(a)=f(b)$. And once we have chosen $f(a)$ and $f(b)$, there are only $3$ allowed values for $f(c)$. Thus there are $(5)(4)(3)$ one to one functions from $A$ to $B$.
For the second problem, let our $2$-element set $B$ be, say, $\{0,1\}$. There are, by the same argument as before, $2^{10}$ functions from a set $A$ of $10$ elements to the set $B$.
How many of these $2^{10}$ functions are bad (not onto)? Exactly $2$ of them, the function that send everybody to $0$ and the one that sends everybody to $1$.
If $m\geq4$, and $f:\>A:=[m]\to B:=[m-2]$ is a surjective map then all elements of $B$ have exactly one preimage, with the following exceptions: either (i) exactly one element $y\in B$ has three preimages, or (ii) two elements $y_1$, $y_2\in B$ have two preimages each.
In case (i) we can choose this $y\in B$ in $m-2$ ways, its preimages in ${m\choose 3}$ ways, and then map the remaining $m-3$ elements of $A$ bijectively onto the remaining elements of $B$ in $(m-3)!$ ways.
In case (ii) we can choose $y_1<y_2$ in ${m-2\choose2}$ ways, then $f^{-1}\bigl(\{y_1\}\bigr)$ in ${m\choose 2}$ ways and $f^{-1}\bigl(\{y_2\}\bigr)$ in ${m-2\choose 2}$ ways. Finally we can map the remaining $m-4$ elements of $A$ bijectively onto the remaining elements of $B$ in $(m-4)!$ ways.
It follows that the number $N$ you are after is given by
$$N=(m-2){m\choose3}(m-3)!+{m-2\choose2}{m\choose2}{m-2\choose2}(m-4)!\ .$$
I may leave the simplification of this result to you.
Best Answer
Obviously if $m<n$, there are no function from $\Bbb A$ onto $\Bbb B$, so assume that $m\ge n$. We’ll use an inclusion-exclusion argument. There are $n^m$ functions of all kinds from $\Bbb A$ to $\Bbb B$. If $b\in\Bbb B$, there are $(n-1)^m$ functions from $\Bbb A$ to $\Bbb B\setminus\{b\}$, i.e., functions whose ranges do not include $b$. We need to subtract these from the original $n^m$, and we need to do it for each of the $n$ members of $\Bbb B$, so a better approximation is $n^m-n(n-1)^m$.
Unfortunately, a function whose range misses two members of $\Bbb B$ gets subtracted twice in that computation, and it should be subtracted only once. Thus, we have to add back in the functions whose ranges miss at least two points of $\Bbb B$. If $b_0,b_1\in\Bbb B$, there are $(n-2)^m$ functions from $\Bbb A$ to $\Bbb B\setminus\{b_0,b_1\}$, and there are $\binom{n}{2}$ pairs of points of $\Bbb B$, so we have to add back in $\binom{n}2(n-2)^m$ to get
$$n^m-n(n-1)^m+\binom{n}2(n-2)^m\;,$$
which can be expressed more systematically as
$$\binom{n}0n^m-\binom{n}1(n-1)^m+\binom{n}2(n-2)^m\;.$$
Unfortunately, this over-corrects in the other direction, by adding back in too much. The final result, when the entire inclusion-exclusion computation is made, is
$$\sum_{k=0}^n(-1)^k\binom{n}k(n-k)^m\;,$$
which can also be written $$n!{m\brace n}\;,$$ where ${m\brace n}$ is a Stirling number of the second kind. The Stirling number gives the number of ways of dividing up $\Bbb A$ into $n$ non-empty pieces, and the $n!$ then gives the number of ways of assigning those pieces to the $n$ elements of $\Bbb B$.