There are a number of reasons for a flourishing sex industry in Thailand:
1. During the Vietnam War, Western soldiers and other personnel would take R&R in Thailand - this, obviously, increased demand for prostitutes in Thailand. The kind of money been thrown around for sex made prostitution a lucrative profession for many.
2. In an effort to industrialise (and at the recommendation of the World Bank & IMF), the Thai government started to push Thailand as an exotic destination for Western tourism - however, little/nothing was done to change the image Thailand inherited from the Vietnam war as being an anything-goes sex and drugs haven - to this day, in fact, many tourist operators advertise Thailand using pictures of scantily clad very young and beautiful Thai women (rather than beaches, temples etc).
3. Buddhist culture requires children (especially girls) to make remittances to their parents as a sign of respect - programmes implemented by the Thai government and international agencies such as the IMF have meant that girls and women, particularly in rural areas, have less opportunity to contribute and make remittances through traditional channels - it can therefore be more acceptable to a thai girl to work as a prostitute and send money to her family than it would be to stay home where work is hard to come by.
4. Although prostitution is illegal in Thailand, for men, buying sex form a prostitute is not exceptional in Thai culture - it is estimated that something like 75% of all Thai boys lose their virginity to prostitutes and it is accepted that a large proportion of married men regularly buy sex.
5. Thailand is surrounded by poorer neighbours - Burmese, Lao, Cambodian and Yunnan Chinese women may all come to Thailand seeking greater opportunity, but if they have entered the country through informal channels, they may only engage in the informal economy - the most lucrative of which may be prostitution.
6. Thailand has long been a hub for drugs- and arms-trafficking - these two industries are closely correlated to human (sex) trafficking - increasing supply and reducing costs for sex purchasers.
7. The relative political and economic stability and superior tourism infrastructure (compared with the aforementioned neighbours), along beautiful natural and cultural attractions and laid-back atmosphere and culture bring (sex) tourists to Thailand. As tourism is Thailand's biggest export, and sex-tourism makes up a not insignificant chunk of that tourism, prosecuting those who buy and sell sex (and women and children for the sex industry) would significantly impact on the Thai economy. Therefore, the Thai government chooses to (largely) turn a blind eye, making sex-tourism relatively risk-free for Westerners in terms of potential legal repercussions.