Factoring a quadratic of the form ax^2+bx+c requires that you find two values, j and k, such that jk=ac and j+k=b. Then you replace bx with jx and kx in the original equation. Then you factor the first and second pair of terms.
So in your case...
x^2+6x+8
ac=jk=8 and b=j+k=6 so j and k must be 2 and 4 so your equation becomes:
x^2+2x+4x+8 now factor 1st and 2nd pair of terms...
x(x+2)+4(x+2) which is equal to:
(x+4)(x+2)
....
x^2-3x-18, ac=jk=-18, b=j+k=-3, so j and k must be -6 and 3 so
x^2+3x-6x-18
x(x+3)-6(x+3)
(x-6)(x+3)
...
x^2-7x+12, jk=12, j+k=-7, so j and k must be -3 and -4 so
x^2-3x-4x+12
x(x-3)-4(x-3)
(x-4)(x-3)