We all read the horror stories about how people were denied entry and deported simply based on the fact that they "stayed in Thailand too long". But what are the legal grounds for that? From what I read, in these situations immigration officials falsify the reason for deportation, citing "insufficient funds" or similar, where the person clearly meets all the legal requirements (holds enough money on them, has an hotel booking, return ticket etc.). There is no official rule about "staying in Thailand too long", that's why they need to make up some false reason.
Of course, not everyone who gets a hard time with immigration at an airport gets deported. Often they are allowed entry after an interrogation. I had a similar experience myself. I was allowed entry but was offered a "special service" for the future - a guaranteed entry for $100. The officer even showed me his phone with a lot of customers paying for this service. This means that they create these obstacles to make people pay them.
What can we do in such situations? As law-abiding tourists, we don't overstay, we don't work in Thailand illegally. Our only "crime" is that we stay in Thailand too long, which is ridiculous. We need to assert our rights. There is an immigration hotline number, which we can call if our rights are violated. In case of unlawful deportation, we have time to file an appeal. And if we encounter a fact of corruption ($100 for entry), we should report it.