Just the ball hitting the table causes spin. It is travelling forwards, but, when it hits the table, the table causes friction on the bottom of the ball, causing the top of the ball to turn forwards.
Therefore, the bottom of the ball spins backwards. The faster the ball moves, the faster it spins, as there is more momentum.
The angle also makes a difference. If it bounces completely square, no spin will be generated.
On the left, it shows all these things represent the same type of spin.
The main reason spin is generated is off the racket. This works exactly the same as when it bounces off the table, except this is controllable. In this case, the friction point, when the racket comes in contact with the ball, is not always the bottom. The racket moves as well as the ball. The spin is not necessarily generated by the movement of the ball, but more so by the movement of the racket.
The movement of the racket changes the direction of the spin. At this angle, if the racket was still, the ball would have spun the other way. The movement of the racket completely reverses this. You can see that the racket moves with the direction of the ball, causing it to spin in that direction.
When the racket moves up, the friction point turns the same way, turning the opposite side the other way. The ball is now spinning. The same thing happens, regardless of the direction of the racket. The ball will also follow the direction of the racket slightly.