Why is being onto necessary for a function to have inverse? I know that a function needs to be one-to-one so that it can have an inverse but could someone please explain why a function (in addition to being one-to-one) needs to be onto so that it can have inverse?

We define the function $f:A\rightarrow B$ as a rule that assigns for
  each $a\in A$, one specific member $f(a)\in B$. The range is denoted
  by $f(A)$ and is the set:
$$f(A)=\{f(x)|x\in A\}$$ Generally, $f(A)\subset B$ and if
  $f(A)\subseteq B$, then $f$ is said to be onto $B$.

 A: If you have $f:A\longrightarrow B$ and if it has in inverse, the inverse must be a function $g:B\longrightarrow A$. If you want $g$ to satisfy the definition of a function, then for each $b\in B$, $g(b)$ must exist, and you must have $f(g(b))=b$. So there must exist some $a\in A$ satisfying $f(a)=b$. What we have here is the definition of $f$ being onto.
Remark that if $f$ is not onto, you can always make it onto by restricting its codomaine to $f(A)$ (strictly speaking it's not the same function anymore, but we still denote it by $f$). So in practice, the real problem is often $f$ not being one-to-one.
A: In general, there are four properties of functions that are typically important (this is likely much more than what the OP asked for, but perhaps it will be helpful in the future).  The properties below are presented in related pairs.


*

*Well-defined.  A relation is well-defined provided "if $a=b$, then $f(a)=f(b)$".  This means that for any one input, there is exactly one output.  When $f$ is given as a formula, this property is usually direct.  On the other hand, when $f$ is given implicitly or with a relation, this property must be checked.  In terms of relations, we must prove that if $(a,b),(a,c)\in f$, then $b=c$.  Being well-defined makes a relation into a function.

*Injective (also called 1-1, one-to-one, into, or mono).  A function (which must be well-defined) is injective provided "if $f(a)=f(b)$, then $a=b$.  This means that each input has a unique output.  This property needs to be checked in almost all situations as it is not usually obvious.  In terms of relations, we must prove that if $(a,c),(b,c)\in f$, then $a=b$.  Being injective makes the inverse relation a function.

*$f:A\rightarrow B$.  This property means that $f$ is a function with domain $A$ and codomain $B$.  In other words, the only valid inputs are in $A$, every element of $A$ is a valid input, and all outputs are in $B$.  When $f$ is given as a formula this means that every element of $A$ must be checked to make sure that the function makes sense for all elements of $A$.  This does not mean that every element of $B$ is an output (it is possible that $f$ misses some of the elements of $B$).  In terms of relations, one must check that (1) for all $(a,b)\in f$, $a\in A$ and $b\in B$ and (2) for all $a\in A$, there exists a $b\in B$ such that $(a,b)\in f$.  When $f$ is invertible ($f$ is injective), this tells that the inverse of $f$ is surjective (but the domain of the inverse might not be all of $B$ because some elements of $B$ are missed).  This property is dependent on the specified domain and codomain.

*Surjective (also called onto or epi).  A domain $A$ and codomain $B$ must be specified for this property.  A function $f:A\rightarrow B$ is surjective if it has the property "for all $b\in B$, there exists $a\in A$ such that $f(a)=b$."  In other words, we can always solve $f(x)=b$ or that every element of $B$ is an output of the function (in other words, no elements of $B$ are missed by $f$).  In terms of relations, we must prove for all $b\in B$, there is an $a\in A$ such that $(a,b)\in f$.  When $f$ is invertible ($f$ is injective), surjectivity means that the domain of the inverse is $B$.  Moreover, when $f:A\rightarrow B$ is invertible ($f$ is injective) and $f$ is surjective, then $f^{-1}:B\rightarrow A$ (so the domain is $B$ and the codomain is $A$) and is surjective.
To make a long story short, you do not need surjectivity for the inverse of a  function to be a function (only injectivity is needed).  You do need surjectivity to specify the domain of the inverse function (usually an inverse function is required to have domain equal to the codomain of $f$.
A: It depends on what your definition of an inverse function is.
If the inverse $g$ of the function $f : S \to T$ is only required to 'undo' $f$ on $S$, namely that $g(f(x)) = x$ for any $x \in S$, then you only need that $f$ is injective.
If the inverse $g$ of the function $f : S \to T$ is additionally required to work on every element of $T$, and that $f$ also 'undoes' $g$ on $T$, namely that $g(f(x)) = x$ for any $x \in S$ and also $f(g(y)) = y$ for any $y \in T$, then you need $f$ to be bijective (both injective and surjective).
A: Basically, if $f$ is not surjective, then there are things in the codomain whose image is not specified by $f$.
Consider the function $f: \{ 1 \} \to \{ 1, 2 \}$ by $1 \mapsto 1$.
Its inverse would be a function $g: \{1, 2 \} \to \{1\}$ such that $f(g(x)) = x$ for each $x \in \{1, 2\}$.
We need to specify what $g$ does at $1$ and at $2$. At $1$ is easy: $f(g(1)) = 1$, and the only thing $f$ sends to $1$ is $1$, so $g(1)$ must be $1$.
What about at $2$?
The $f(g(x)) = x$ condition precisely says that whatever $g$ does to $2$, $f$ needs to send it to $2$. But $f$ never sends anything to $2$ at all!
Because $f$ was not surjective, it was not possible to define $f$'s inverse at a value $f$ did not hit.
