By the inclusion-exclusion principle, a Set $B$ containing $N$ distinct objects, with set of properties $B_1, B_2, \ldots, B_n$ (that the objects of the set $B$ may possess), and $N(B_i)$ being the number of objects having property $B_i$,the number of objects in $B$ having none of the properties $B_1, B_2, \ldots, B_n$ is given by
$N\left(\bigcap\limits_{i=1}^{n}\bar{B_i}\right)=N-N\left(\bigcup\limits_{i=1}^{n}B_i\right)$
$=N-\left(\sum\limits_{i=1}^{n} N(B_i)-\underset{1\leq i\neq j \leq n}{\sum\sum}N(B_i\cap B_j)+\underset{1\leq i\neq j \neq k \leq n}{\sum\sum\sum}N(B_i\cap B_j \cap B_k) - \ldots + (-1)^n N\left(\bigcap\limits_{i=1}^n B_i\right)\right)$
Now, for the mapping $f: A \to B$, with $A=\{a_1,a_2,\ldots,a_m\}$ and $B=\{b_1,b_2,\ldots,b_n\}$, s.t., $|A|=m$ and $|B|=n$, with $m\leq n$, let's define the property $B_i$ as $b_i \not \in B$, $\forall{i\in\{1, 2, \ldots, n\}}$, with $N=n^m$ being total number of possible mappings.
Then we have the number of onto functions
$=N\left(\bigcap\limits_{i=1}^{n}\bar{B_i}\right)$
$=n^m-\left(n(n-1)^m-{n \choose 2}(n-2)^m+\ldots+ (-1)^{n-1}{n \choose n-1}1^m\right)$
$=\sum\limits_{k=0}^{n}(-1)^k{n \choose k}(n-k)^m$
(Notice the similarity of the above expression with that of The Matching Problem/Derangements - n letters to n people)