You can buy the Re-Entry Permit either at a provincial Immigration Office or in the departure hall of the main Thai international airports.
Buying it at Suvarnabhumi Airport is very quick and easy. It takes less than 10 minutes, as you ONLY need to show your passport with your current Permit to stay from the Non Imm O Visa.
But there are TWO very important points to take into account when you want to buy it there:
#1 - You don't need to fill out the Re-Entry Permit form beforehand, nor provide a photograph. The Airport Immigration service fills out the form for you based on your Passport details and takes a picture of you.
But you need to bring 1.200,- THB in cash, as they do not accept debit- or creditcards to pay for the service (1.000 THB being the standard price for the One Time Re-Entry Permit, and 200 THB for the administrative service)
#2 - At Suvarnabhumi there are 2 entrances for International Departures. You should take entrance 2, as it is there that the Re-Entry Permit service is located. After you passed the 'screening' of your hand-luggage, and go to the Immigration Stamp-Out hall, the Re-Entry Permit service is indicated and it is at the left-side of the various counters where you are being stamped out. So you should go for theRe-Entry Permit first, and once you have it then proceed to one of the counters to get stamped out.
Note: I am pretty sure that if you only realize that you forgot to apply for the Re-Entry Permit and are already 'stamped out' (which invalidates your Permit to stay) that Immigration would allow you to still get it < in Don Mueang Airport - the other Bangkok international airport - the Re-Entry Permit service is located after you are stamped out >, but might be useful to put a Post-It note in your passport with RE-ENTRY PERMIT in your passport, in case you would forget, so that the Immigration officer will draw your attention to do it before him stamping you out.