This formula can be derived in various ways, and with connections to different areas of mathematics. One way of seeing why it generates all the triples is to note that the unit circle $$x^2+y^2=1$$ in polar co-ordinates has $y=\sin \theta, x=\cos \theta$ so this takes in all the points of the circle (for angles $-\pi\le \theta \lt \pi$ it traverses the circle once).
Now, with $t = \tan \frac {\theta}2$ we have the half angle formulae $y=\dfrac {2t}{1+t^2}, x=\dfrac {1-t^2}{1+t^2}$ which parametrise the unit circle with rational functions (the point at $x=-1, y=0$ corresponds to the singularity in the tangent function).
If we substitute back into the original equation for the circle, and multiply through by $(1+t^2)^2$ we get a pythagorean relationship $$(1-t^2)^2+(2t)^2 = (2t)^2+(t^2-1)^2=(t^2+1)^2$$
We can do this for every rational point on the unit circle, and for each integer $t$ we recover such a rational point.
There are "more elementary" ways of showing how pythagorean triples work, but this is worth noting as an example of the rational parametrisation of a curve - a topic which has its own theory, generalising the case of the circle. You can draw a diagram to show how a pythagorean triangle with integer sides corresponds to a rational point on the unit circle.