Represent the digits in a number N by a, b, c, etc. For example, a 4-digit number would be abcd. Add the digits together: a+b+c+d. The number N itself is 1000a+100b+10c+d=999a+a+99b+b+9c+d, which can be written: 999a+99b+9c+a+b+c+d=9(111a+11b+c)+a+b+c+d. Divide this by 9: 111a+11b+c+(a+b+c+d)/9.
111a+11b+c is a whole number because 9 divides exactly into 9(111a+11b+c). Therefore to test divisibility of N by 9 we only need to add the digits together (a+b+c+d) and see if this sum is divisible by 9.
Since 3 is a factor of 9, N=3(333a+33b+3c)+(a+b+c+d). The first part of this divides exactly by 3, so N is divisible by 3 if a+b+c+d is also divisible by 3.
What applies to a 4-digit number applies to numbers with any amount of digits because one less than any power of 10 is just a series of 9s which is always divisible by 9, which means it's also divisible by 3.