When TeX expands
\the\count0, it sees that the thing to be printed is the contents of a
\count register, therefore it looks for a 〈number〉 after
\count (the number of the
\count register to be printed). This “looking for” process consists in expanding all tokens from the input stream until finding an unexpandable token that doesn't fit in the grammar for a 〈number〉 given in the TeXbook p. 269. A 〈space token〉 always ends this process and becomes part of the 〈number〉 (it is gobbled by the 〈number〉 scanning process), but no more than one—see Grammar complements below. A
\relax token also ends the process but doesn't become part of the 〈number〉, so TeX will use it for further processing of the input stream.
Note: in many cases,
\relax does nothing, but in some cases, it may end particular processes of TeX (looking for
\omit at the beginning or end of an
\valign] row [resp. column]).
So, when you write
\the\count0 \the\count1, the first
\the applies to
\count0 because the space following
0 ends a 〈number〉.
\the\count0 (with ending space) always expands to the contents of count register 0 and adds no spurious space to the input or horizontal list.
\the\count1 prints... something that depends on what follows the
1. If more digits follow (possibly after expanding macros), the number of the register to be printed can be larger than 1. With a single quote following the
1 as in your example, the 〈number〉 is terminated and the contents of count register 1 is printed. When you can't be sure about what follows (macros not under your control), terminate the 〈number〉 with a space token; it will be gobbled.
\relax is a popular alternative and always finishes a 〈number〉, but as pointed out in the above note, it then remains in the input stream, which may have undesirable consequences in some particular cases (see towards the end of this answer).
When you write
\the\count0\the\count1', things are different for the first
\the\count, because after
0, TeX is still expanding tokens looking for the next digits of the 〈number〉 that started with
\the is always expandable, it is expanded.
\the\count1 expands to
34 in your example; as a consequence, this
34 becomes part of the first 〈number〉, which is
034, i.e. decimal 34 since the 〈number〉 didn't start with a single or double quote that would indicate octal or hexadecimal notation. Thus, in your example:
(with no space inside) prints the contents of count register 34 followed by an apostrophe, which you can verify by setting for instance
\count34=77 (followed by a space token or by
\relax) earlier in your document. Of course, in normal operation, you shouldn't write to
\count registers without first making sure that they aren't already used for something else (see “scratch registers”).
After unrolling the first grammar production rules for 〈number〉 (see TeXbook p. 269; these allow for optional plus, minus signs and spaces at the beginning of a 〈number〉, as well as coercion of an 〈internal dimen〉 or 〈internal glue〉 into a 〈number〉), you'll arrive at the production rules for a 〈normal integer〉:
〈normal integer〉 → 〈internal integer〉
| 〈integer constant〉〈one optional space〉
| '〈octal constant〉〈one optional space〉
| "〈hexadecimal constant〉〈one optional space〉
| `〈character token〉〈one optional space〉
` are character tokens of category code 12 (“other”), 〈one optional space〉 is defined by:
〈one optional space〉 → 〈space token〉 | 〈empty〉
and 〈integer constant〉 matches any non-empty sequence of decimal digits with category code 12. The production rule
〈normal integer〉 → 〈integer constant〉〈one optional space〉
(one of those for 〈normal integer〉) is used for all 〈number〉s of your example, and the 〈space token〉 is the one we mentioned above, that becomes part of the 〈number〉 when present (but only one).