That quite depends on the bot. There are various systems that go about it in different ways. The simplest and most common is called “pattern matching” and works like a lookup table. The bot’s creator first makes a list of all expected inputs and writes them down along with default answers to return.
input: Hello.
answer: Hello to you to.
input: How are you?
answer: I am fine.
input: Yes or no?
answer: Yes.
When the bot gets an input, it simply looks it up in the list and responds with the corresponding default answer. Every answer is manually rigged. Of course, you can never anticipate every exact input, so the list also has answers in response to just a few keywords. And when the list of answers does not contain any matching input or keyword to what the user said? Then it simply fails, and usually the bot gives a random default answer to change the topic.
This can be made increasingly more complex though. Some systems allow you to finetune the list with grammatical data (e.g. give a different answer to the verb “wind” and the noun “wind”). You could also take the subject of an input phrase, whatever it is, look up some facts about it in a database, and give that fact as answer. (e.g. “What is a X” -> look up definition of X in a database or wikipedia and return that as answer). As Andrew mentioned in the other topic, Cyc is one such database, but also WordNet. You could also add programming to make the bot draw inferences between simple facts. That’s about as far as conventional chatbots go.
Then there’s the statistical approaches, which again, use a list of answers. A good example is Cleverbot, who learned (copied) all its answers from humans who chatted to it. It also remembers which answer was typed in response to which answer, and so pretty much parrots its users. However, when it has multiple answers on file for the same input, it chooses the best one by scoring each listed answer by how many of the same words it contains, and/or how often it saw a human give that answer. Technically you could add all sorts of underlying data to this statistical score system, like emotional values associated to certain words. If the user enters a sentence with words that indicate anger (there are word lists for this called sentiment databases), the bot could give a higher score to answers that also contain angry words. The highest scoring answer is then chosen.
And then there is artificial intelligence, which is too broad a field to explain easily and much less used in chatbots. Though technically Cleverbot’s use of a neural network for its scoring system is also artificial intelligence, and pattern matching can be augmented with it.