x^3-3x^2+3x-9=0. My book factors it to x^2(x-3)+3(x-3)=0, but then it magically becomes (x-3)(x^2+3). I have no idea how it became these numbers. Could you explain please? Thanks!

you have a common factor, x-3. Let me use w for it.

x^2*w+3w=
w(x^2+3)
(x-3)(x^2+3)