This is because the "naive" approach to e.g. a digital square wave is to sample a logically continuous function defined as:
if (less than halfway through the cycle period) -1
else 1
Firstly, this is almost always going to be out of tune because the transitions from -1 to 1 and 1 to -1 necessarily fall across a digital sample boundary.
For any frequency not evenly dividing the sample rate the transition will be slightly early or late.Secondly, the sharp rising edge of the square wave, from a "sum of sines" perspective, requires sinusoid components that are not part of a conventional square wave to represent them digitally. If you were to digitally sample an analog square wave through an ADC it would look quite different from the result of this function.
The naive digital sawtooth wave suffers from similar issues due to its discontinuity.