The basic idea is that identities such as $(1)$ come from using the formula $$\arctan x + \arctan y = \arctan \frac{x + y}{1 - xy}.$$ Since $\frac\pi4 = \arctan 1$, we have $$\frac\pi4 = \arctan x + \arctan \frac{1-x}{1+x}$$ for any $x$ (solving for the $y = \frac{1-x}{1+x}$ that will give us $\frac{x+y}{1-xy}=1$). So we might write $$\frac\pi4 = \arctan \frac12 + \arctan \frac13$$ for example. Then we could break up each of these arctangents into smaller arctangents, and keep going.
So in fact we get lots and lots of identities. Which ones are the best ones? For speed of convergence, it's obviously best to have a very small value of $x$ in $\arctan x$; maybe $x = \frac1n$ for a pretty large integer $n.$ But it would also be nice if we could "double up" on a term: what's good about $(1)$ is that $\arctan \frac15$ is there four times, but we only need to compute it once.
We get the same arctangent twice by using the formula $$2 \arctan x = \frac{2x}{1-x^2} \qquad \Longleftrightarrow \qquad 2 \arctan \frac pq = \arctan \frac{2pq}{q^2-p^2}.$$
We can't make this work with $\frac{2pq}{q^2-p^2} = 1$, but we can make this very close to $1$, so that the leftover part is the arctangent of something very small. You might notice that $2pq$ and $q^2-p^2$ are two sides of a Pythagorean triple: $(2pq)^2 + (q^2-p^2)^2 = (q^2 + p^2)^2$. So we can't make them equal, because there's no isosceles right triangle with integer sides. But by solving Pell's equation, we can make them consecutive integers. More precisely, if we have a solution to $x^2 - 2y^2 = -1$, then we also have $\left(\frac{x-1}{2}\right)^2 + \left(\frac{x+1}{2}\right)^2 = y^2$, which we can use to get a nice double-arctangent identity.
For example, if we take $(x,y) = (7,5)$ (which satisfies $7^2 - 2\cdot 5^2 = -1$) then we get the Pythagorean triple $(3,4,5)$, which has $q=2$ and $p=1$. So this gives us the identity $2 \arctan \frac12 = \arctan \frac43$. When $x = \frac43$, $\frac{1-x}{1+x} = -\frac17$, so we have $$\frac\pi4 = \arctan \frac43 - \arctan \frac17 = 2\arctan \frac12 - \arctan \frac17.$$ If we take $(x,y) = (41,29)$, we get the Pythagorean triple $(20, 21, 29)$, which has $q=5$ and $p=2$, this gives us the identity $2 \arctan \frac25 = \arctan \frac{20}{21}$. When $x = \frac{20}{21}$, $\frac{1-x}{1+x} = \frac{1}{41}$, so $$\frac\pi 4 = \arctan \frac{20}{21} + \arctan \frac1{41} = 2 \arctan \frac25 + \arctan \frac{1}{41}.$$ Since $\frac25 \approx \frac13$, we could try to write $\arctan \frac25 = \arctan \frac13 + \arctan y$, and get $y = \frac1{17}$. So we can rewrite the identity as $$\frac\pi4 = 2 \arctan \frac13 + 2\arctan \frac1{17} + \arctan \frac1{41}.$$
We get the infamous formula with $239$ in it by taking $x=239$ and $y = 169$ as our Pell solution, which gives us the Pythagorean triple $119^2 + 120^2 = 169^2$ and $(p,q) = (5,12)$. So $2 \arctan \frac{5}{12} = \arctan \frac{120}{119}$; when $x = \frac{120}{119}$, $\frac{1-x}{1+x} = -\frac{1}{239}$, so we get the identity $$\frac\pi4 = 2\arctan \frac{5}{12} - \arctan \frac{1}{239}.$$
Here, we get extra lucky, because $5$ and $12$ form a Pythagorean triple themselves, so we can write $\arctan \frac{5}{12} = 2 \arctan \frac15$, giving you the identity $(1)$.