I am constantly getting knots in my back from working out, even though I do stretch a lot (it's worse if I don't). What can I do to prevent knots?

Technique, balance. To start with, it would be helpful to have your technique and lifting program evaluated by a trainer. Over-recruitment of certain sections of a muscle and imbalance between opposing muscle groups can often lead to knots (i.e. Overdevelopment of the pecs in relation to the rhomboids). If this does not help, you should be evaluated by a sports medicine physician to rule out other systemic causes.
Nasty Knots. It sounds as if you are possibly overtraining. Increased spastic pain after a period of rest following a vigorous work out suggests excessive microtrauma to the muscle tissue. Repeat exercise or stretching increases blood flow to help with localized wash-out of Lactic Acid that builds up after vigorous workouts which is why if feels better...Temporarily. Avoid overtraining and rest properly.