It’s rather simple. If the two fragments of the word are next to each other in the way they would be in an infinitive, then connect them into one word. Otherwise, leave them apart.‡ This also gives you a hint to the pronunciation: If they are written together, pronounce it as one word, otherwise as two.
In essence, you need to remember that only the inflecting part of a separable verb is considered the ‘true’ verb. While German word order is essentially free, the finite verb always needs to occupy a certain position which is the second in main clauses and the final in subordinate ones.
The non-verb part then should be put at the very end of the sentence in main clauses and just before the verb in subordinate clauses.
Put together, this means that in infinitives, participles and subordinate clauses (the first two always take the complete verb, as a whole; in the latter, separable part is last but to the verb) the verb is connected, as a finite verb in main clauses it is not.
Ich lege jetzt auf.
Er sagt, er lege auf.
Er sagt, dass er auflege.
Ich habe aufgelegt.
Ich werde auflegen.
Ich will auflegen.
Ich sage das, weil ich auflege.
‡: There is one exception that only really happens in spoken language: To strongly emphasise the separated fragment of a verb, it can be pulled into a main clause’s first position with the finite part second. However, these are still two words and emphasised as such:
»Was wollen Sie weglegen?« »Auf lege ich!«