Let $f: X \to Y$.
A function $g: Y\to X$ is said to be:
Let $f: X\to Y$. Then:
Let $f: X \to Y$ be injective. Let $x_0$ be any element of $X$. Define $g: Y\to X$ as such: $g(y) = \begin{cases}x \text{ if } y \in \text{range of }f \text{ and } y = f(x)\\ x_0 \text{ if }y \notin \text{range of }f\end{cases}$
$g$ is indeed a function from $Y$ to $X$. This is because if $y$ is in the range of $f$, then there is precisely one $x \in X$ with $y = f(x)$ because $f$ is injective.
Next, $g(f(x)) = x$ because $f(x)$ is in the range of $f$ and the first branch of the definition of $g$ applies to give $g(f(x)) = x$.
Let $f: X\to Y$.
Let $g: Y\to X$ be a right inverse of $f$. So $f(g(y)) = y$ for all $y \in Y$.
Given any $y \in Y$, we must produce an $x \in X$ with $f(x) = y$ to show $f$ is surjective.
If we choose $x = g(y)$, then $f(x) = y$ since $f(g(y)) = y$. So $f$ is surjective since we can produce an $x \in X$ for all $y \in Y$.