How to affiliate papers that I worked on both as a PhD student and as a postdoc?
This is a mathematics specific answer.  My understanding is that conventions may be different in other fields.

The primary purpose (in mathematics) of the affiliation on your paper is to distinguish you from other people with the same name and to provide a way for readers to contact you.  Therefore, you should put your affiliation at the time the paper is submitted.  Furthermore, if you change affiliations between submitting the paper and getting the paper proofs before final publication (which is likely to happen), you should make the change on the paper so that the published version matches your affiliation at the time.

You can (and in my opinion generally should) acknowledge your former affiliation(s) in an acknowledgement section.