Let $f: \mathbb{N}^2 \to \mathbb{Z}$ via $f(n, m) = n-m$ and let $g: \mathbb{Z} \to \mathbb{R}$ via $g(x) = \sqrt{|x|}$
$g \circ f$ is defined since $g \circ f : \mathbb{N}^2 \to \mathbb{R}$
$$\implies g(f(n,m)) = g(n-m) = \sqrt{n-m}$$
I don't get why $ f \circ g$ is not defined?
The codomain of $g$ is $\mathbb{R}$, while the domain of $f$ is $\mathbb{N}^2$, thus $g$ doesn't output the kinds of things that $f$ requires as input. Hence the composition is not defined.
Edited to Elaborate: When we say that $f : \mathbb{N}^2 \to \mathbb{Z}$, we are declaring that $f$ is a function that eats pairs of natural numbers, and spits out integers. If we try to feed $f$ with something that is not a pair of natural numbers, then $f$ chokes.
Next, note that $g : \mathbb{Z}\to \mathbb{R}$. This means that $g$ eats integers, and spits out real numbers. When we compose $g$ with $f$, i.e. when we try to define $g\circ f$, there is no problem. $f$ outputs integers, and $g$ eats them up. Groovy.
On the other hand, when we try to compose in the other way, i.e. to define $f\circ g$, $g$ outputs real numbers, and $f$, which is expecting pairs of natural numbers, chokes. It can't handle what $g$ spits out, so the composition is nonsense.