It's when you say something you don't actually mean, usually the opposite. It is most often humorous although it can also be said to make the other person feel silly or foolish. Tone of voice can be important in conveying sarcasm, so it often works better in speech than when it is written. You may have encountered this in texts or other conversations online, where it can be difficult to figure out a person's tone just from the words.
When writers use sarcasm as part of a larger work, they are often able to do it more effectively than what you see in a quick online exchange of messages because there is the space to create context. Sarcasm is a literary device, and literary devices in turn are a type of figurative language. Figurative language encompasses a wide variety of techniques used by writers but also in everyday speech that draws relationships between images, objects or people. While statements using figurative language are not literal, they often convey the writer's point more powerfully than literal language does.
One important point to remember about sarcasm in everyday speech is that how well it works often depends on your relationship with the person you're using it on. You and a good friend might often be sarcastic to one another, and you both have the context to know you are just kidding around with one another. With someone you don't know well, sarcasm can be more easily misunderstood or could be seen as cruel even if you are just trying to make a joke.
When a character in a piece of fiction is sarcastic, it can tell you something about that person. You can get similar insight into a writer in a personal essay. Sarcasm can also be useful in driving a point home.
Different types of sarcasm are not really formally defined, but if you are trying to figure out if something is sarcastic or not, it can help to have an idea of some of the categories that a remark might fall into.
One way that sarcasm differs from other forms of verbal irony is its intent. It is always deployed with the aim to mock or satirize. Verbal irony also involves saying the opposite of what the speaker means, but it tends to be subtler and gentler. Related literary devices include hyperbole, in which you exaggerate to make a point, and satire, in which you use different types of figurative language in order to mock ideas or people. One of the most important things to remember about all these approaches is that they have to be deployed with caution and skill or your message can be misunderstood.