At its core, HTTP is stateless - each request/response cycle stands on its own, with the server forgetting everything after responding. But often we need to remember things between requests, like user logins or shopping carts. That's where cookies come in.
Cookies are tiny text files stored in your browser. When a server wants to remember something, it sends a "Set-Cookie" header telling your browser to create a cookie. From then on, your browser sends that cookie data back with each request to the server, allowing it to "remember" you.
But it goes further - cookies enable sessions, which are like personal data stores on the server tied to your specific interactions. The server gives you a unique "session ID" cookie, and when you send it back, the server recognizes you and accesses your session data.
There are some security and privacy controls baked in. Same-site cookies only get sent to the originating site to prevent cross-site attacks. Cookies are also isolated by domain and path to limit access. Secure cookies only transmit over encrypted HTTPS, while HttpOnly cookies are hidden from browser JavaScript code to stop cross-site scripting.