For stays of up to thirty days, most foreign passport holders automatically get a free non-extendable transit visa when passing through immigration at Don Muang Airport, at the Malaysian border or at the Laos border, but may have to show proof of onward travel arrangements.
These transit visas are absolutely non-extendable, so you might want to apply for a sixty-day tourist visa instead, obtainable in advance from Thai embassies . In the UK, sixty-day visas take two working days to process if you go in person (Mon-Fri 9.30am-12.30pm), or ten days if you apply by post, and cost £8. In the US, it costs $15 and takes 24 hours in person, or five days by post. In Canada it costs CAN$16.50 and is processed in three working days to a week; in Australia it costs A$18 and takes three to five days. New Zealanders (and nationals of South Korea, Sweden, Denmark, Norway and Finland) with a valid onward ticket get a free ninety-day visa.
All sixty-day tourist visas can be extended in Thailand for a further thirty days, at the discretion of officials; visa extensions cost B500 and are issued over the counter at immigration offices ( kaan khao muang) in nearly every provincial capital - most offices ask for one or two extra photos as well, plus two photocopies of the first four pages and latest Thai visa page of your passport. If you use up the three-month quota, the quickest and cheapest way of extending your stay for a further sixty days is to head down to Malaysia and apply for another tourist visa at the embassy in Kuala Lumpur.
Immigration offices also issue re-entry permits (B500) if you want to leave the country and come back again within sixty days. If you overstay your visa limits, expect to be fined B100 per extra day when you depart Don Muang Airport, though an overstay of a month or more could land you in trouble with immigration officials.