Reciprocal space
One way to derive the formula is to use reciprocal space coordinates. The reciprocal space lattice has three unit cell vectors, a*, b* and c*. The diffraction vector d* is given by:
$$d^* = h a^* + k b^* + h c^*$$
The d-spacing is given by the reciprocal length of d*. The relationship between direct space and reciprocal space unit cell vectors is
$$a \cdot a^* = 1$$
$$b \cdot a^* = 0$$
$$c \cdot a^* = 0$$
$$a \cdot b^* = 0$$
$$b \cdot b^* = 1$$
$$c \cdot b^* = 0$$
$$a \cdot c^* = 0$$
$$b \cdot c^* = 0$$
$$c \cdot c^* = 1$$
So to get the d-spacing, we have to figure out the length of $d^*$. For a cubic system, the reciprocal space unit vectors all have the lenght of 1/a, and they are all at right angles. To get the length for the cubic system, we use Pythagoras' theorem (or the length of a vector in Cartesian space): It is the square root of the coordinates squared.
So:
$$|d^*| = |a^*| \sqrt{h^2 + k^2 + l^2}$$
To get to your formula, substitute 1/d for $d^*$ and 1/a for $a^*$. For the hexagonal system, it is a bit different because the angles are not all 90 degrees, so there will be a trigonometric factor (sin 120 or cos 120 or such) in the respective formula.
Direct space
One plane will go through the origin, the next through a/h, b/k and c/l. In the cubic case, a vector normal to the plane is (h, k, l). To get the distance of the plane from the origin, you can take any point in the plane and form the dot vector with the normalized vector (h, k, l). For example,
$$ d = \frac{(h, k, l)}{\text{length} (h, k, l)} \cdot (1/h, 0, 0)$$
For a cubic system, this will simplify to the expression for the d-spacing. The term $h^2 + k^2 + l^2$ is from the length of the (h, k, l) vector.
(h k 0), no vectors
Both derivations above use vectors. Here is a derivation for the 2-dimensional case using no vectors.

Let the origin be point A. In a cubic system, the plane hk0 intersects the x-axis at a/h (point B) and the y-axis at b/k (point C). Our goal is to calculate the altitude. We use that the area of the triangle can be calculated two ways, using the perpendicular sides or using the hypothenuse and altitude:
$$\text{Area} = \frac{1}{2} * \frac{a}{h} * \frac{b}{k} = \frac{1}{2} * \text{hypothenuse} * \text{altitude}$$
We can get the length of the hypothenuse from Pythogoras:
$$\text{hypothenuse} = \sqrt{(\frac{a}{h})^2 + (\frac{b}{k})^2}$$
Then, we can solve for the altitude:
$$\text{altitude} = \frac{\frac{a}{h} * \frac{b}{k}}{\text{hypothenuse}}$$
$$ = \frac{\frac{a}{h} * \frac{b}{k}}{\sqrt{(\frac{a}{h})^2 + (\frac{b}{k})^2}}$$
With a = b and simplifying, we get the formula the OP asked for (in two dimensions).