Sum of squares, confusion along the way in getting the formula. See pages 4-5 of these notes here on calculus by Nets Katz.

Before leaving Gauss' proof, let us at least examine how it generalizes to sums of squares. Let us consider$$S_2(n) = 1 + 4 + \ldots + n^2 = \sum_{j = 1}^n j^2.$$In order to calculate this sum, a la Gauss, it helps to have a geometric notion of the number $j^2$. It is in fact the number of pairs of natural numbers less than or equal to $j$. In set theoretic notation$$j^2 = \#\{(l, m): l, m \in \mathbb{N},\,l,m \le j\}.$$Thus we can write $S_2(n)$ as the number of elements of  a set of triples. Basically we use the third component of the triple to write down which term of the sum the ordered triple belongs to.$$S_2(n) = \#\{(j, l, m) : j, l, m \in \mathbb{N},\,l, m\le j,\, j \le n\}.$$Thus the number we seek, $S_2(n)$ is the number of triples of natural numbers less than or equal to $n$, so that the first component is greater than or equal to the last two components. Gauss' trick generalizes to the following observation. For any ordered triple, one of the components is at least as big as the other $2$. This suggests we should compare $3$ copies of $S_2(n)$ to $n^3$ which is the number of triples of natural numbers less than or equal to $n$. But we have to be careful, we are counting triples where there are two components larger than the third twice and we are counting triples where all three components are equal three times.
Now observe that the number of triples of natural numbers less than or equal to $n$ with all components equal, formally$$\#\{(j, j, j) : j \in \mathbb{N},\,j \le n\}$$is just equal to $n$, the number of choices for $j$. It is also easy to count triples that have the first two components large and the third smaller. We observe that$$\#\{(j, j, l) : j, l \in \mathbb{N},\,j \le n,\,l \le j\} = S_1(n).$$We get this because for each $j$ there are $j$ choices of $l$, so we are summing the first $n$ numbers. Then like Gauss we observe that each triple with two equal components at least as the third, has the third component somewhere. Combining all these observations we can conclude that$$n^3 = 3S_2(n) - 3S_1(n) + n.$$ (Basically the first term correctly counts triples with all different components. The first term double counts triples with two equal components and one unequal
but the second term subtracts one copy of each of these. The first term
triple counts triples with all components the same, but the second term also
triple counts them, so we have to add $n$ to correctly account for all triples.)

Question. It's still not clear to me at all how we got$$n^3 = 3S_2(n) - 3S_1(n) + n.$$I understand to some extent how we got the $3$ in front of the $S_2(n)$, but it's not clear to me at all how the $3$ in front of the $S_1(n)$ popped up and why we're adding an $n$. Sure, the parenthetical in the notes following the expression$$n^3 = 3S_2(n) - 3S_1(n) + n$$sort of gives an explanation, but it doesn't really let me know why we end up getting $3$, $-3$, and $1$ as the specific coefficients in front of the $S_k(n)$. Certainly the inclusion-exclusion principle is lurking around here, but I suffer from a lack of combinatorial skill, and the inclusion-exclusion principle hasn't been introduced by this point in the notes, much less proved. So could anyone add some more details, more intuition as to how they might come up$$n^3 = 3S_2(n) - 3S_1(n) + n?$$Thank you!
Update. John Griffin gave a very excellent answer, and I'm still trying to digest it. But is there a way of phrasing what he said in terms of the framework of the inclusion-exclusion principle? Also, is there a way, or I guess, "tricks", for visualizing the proof given in the notes of Katz?
 A: We begin by noticing
$$S_2(n) = \#\{(j, l, m) : j, l, m \in \mathbb{N},\,l, m\le j,\, j \le n\} \tag{1}$$
and
$$ n^3 = \#\{(j, l, m) : j,l,m\in\mathbb{N},\,j,l,m\leq n\}. \tag{2}$$
We can write $S_2(n)$ two more ways; namely,
$$S_2(n) = \#\{(j, l, m) : j, l, m \in \mathbb{N},\,j, m\le l,\, l \le n\}
\tag{3}$$
and
$$S_2(n) = \#\{(j, l, m) : j, l, m \in \mathbb{N},\,j, l\le m,\, m \le n\}
\tag{4}. $$
Since each term in the set in $(2)$ has a maximum component, it lies in either of the sets in $(1)$, $(3)$, or in $(4)$. This means that we can get close to counting $n^3$ by $3S_2(n)$. However, this is only getting "close" because some terms in the set in $(2)$ may lie in more than one of these sets in $(1)$, $(3)$, and $(4)$. Thus we need to subtract something to correct this multiple counting:
$$
n^2 = 3S_2(n) - (\text{something})
$$
So lets find which terms are being counted multiple times. If $n=10$, then a term like $(4,4,4)$ would be counted three times. Also, a term like $(3,3,2)$ would be counted twice, as it would lie in both $(1)$ and in $(3)$. Similarly $(3,2,3)$ and $(2,3,3)$ would each be counted twice.
In general, we consider the counts
$$
\#\{(j,j,j) : j\in\mathbb{N},\,j\leq n\} \tag{5}
$$
and
$$
\#\{(j,j,l):j,l\in\mathbb{N},\,j\leq n,\,l\leq j\}
\tag{6}$$
and
$$
\#\{(j,l,j):j,l\in\mathbb{N},\,j\leq n,\,l\leq j\}
\tag{7}
$$
and
$$
\#\{(l,j,j):j,l\in\mathbb{N},\,j\leq n,\,l\leq j\} \tag{8}.
$$
The first of these is equal to $n$, and the other three are equal to $S_1(n)$.
If we subtract $3S_1(n)$, then we will have completely corrected the double counting. However we would also completely eliminate the consideration of terms of the form $(j,j,j)$, because such a term would be first counted triple by $3S_2(n)$ and then also triple by $3S_1(n)$. Thus we need to add $n$ to correct.
Therefore
$$
n^3 = 3S_2(n)-3S_1(n)+n.
$$

This can also be phrased in terms of the inclusion-exclusion principle. Let $A_1$, $A_2$, and $A_3$ denote the sets in $(1)$, $(3)$, and $(4)$, respectively. By the inclusion-exclusion principle, we have
$$
|A_1\cup A_2\cup A_3|
=|A_1|+|A_2|+|A_3|-|A_1\cap A_2|-|A_1\cap A_3|-|A_2\cap A_3|+|A_1\cap A_2\cap A_3|.
$$
(Here it is much nicer to use $|\cdot|$ instead of $\#(\cdot)$ to denote the cardinality of a set.)
Since $|A_i|=S_2(n)$ and $|A_i\cap A_j|=S_1(n)$ for $i\ne j$, $|A_1\cap A_2 \cap A_3|=n$, and $|A_1\cup A_2\cup A_3|=n^3$, the above equation becomes
$$
n^3 = 3S_2(n) - 3S_1(n) + n.
$$
