Usually you are trying to solve a particular problem, where a solution would help people out. You use things you have seen, things you have read, things you were taught and then when you are faced with a problem you draw on these experience to come up with new answers and make new things.
I agree.. I think about a problem while I am working on something particular. If it is an interesting problem, I tend to think about it on my way home from work, dream about it, eat my breakfast with it, etc… Sometimes the answer just pops into my head spontaneously. Other times I have to work it out on the chalk board with help from my supervisor and lab mates.