When to use "cannot" versus "can't"?

Grammatically, you can use can't instead of can not or cannot in the majority of circumstances. There is an exception. In wh-movement, the contraction should not be expanded unless you also change the word order:

Why can't I have some bacon? //OK
Why cannot I have some bacon? //not OK, archaic
Why can I not have some bacon? //OK again, although formal

Stylistically, the choice between can't and cannot is more complex. Generally, people use can't in speech and informal writing, and cannot or can not in formal writing or very formal speech. Also (as @Kris points out in a comment), cannot might be used when you need to carefully distinguish it from can't in speech.