Let $A:=\{ x_1,..,x_n \}$ and $B=\{y_1,..,y_m \}$.
Lets count the number of onto functions $f:A \to B$. There are $m^n$ functions from $A$ to $B$. Lets count now the ones which are not onto:
Define
$$P_i= \{ f : A \to B |y_i \notin f(A) \}$$
Then we need to figure out the cardinality of $\cup_i P_i$.
By the inclusion exclusion principle
$$|P_1 \cup P_2 ..\cup P_m |=\sum |P_i|-\sum |P_i \cap P_j|+\sum |P_i \cap P_j \cap P_k| -... \\=\binom{m}{1}(m-1)^n-\binom{m}{2}(m-2)^n+\binom{m}{3}(m-3)^n-...
$$
Thus in total there are
$$m^n-\binom{m}{1}(m-1)^n+\binom{m}{2}(m-2)^n-\binom{m}{3}(m-3)^n-...=\sum_{k=0}^m (-1)^k\binom{m}{k}(m-k)^n$$
When $n=m$ the number of onto functions is
$$\sum_{k=0}^n (-1)^k\binom{n}{k}(n-k)^n$$
But any function $f: \{ x_1,..,x_n \} \to\{y_1,..,y_n \}$ is onto if and only if it is a bijection. Thus the number of onto functions is equal to the number of bijections, which is $n!$.
Hence
$$\sum_{k=0}^n (-1)^k\binom{n}{k}(n-k)^n=n!$$