The server sets a cookie by adding a Set-Cookie header to its HTTP response, for example Set-Cookie: session_id=8f3c2a; Path=/; HttpOnly; Secure; SameSite=Lax. The browser reads that header and saves the cookie.
On every following request to that site, the browser automatically adds a Cookie header carrying the values back: Cookie: session_id=8f3c2a. The application does not have to ask for it; the browser sends it on its own. The server reads the value and looks up who that session belongs to.
Attributes decide the rules. Expires or Max-Age sets a lifetime; without one the cookie is deleted when the browser closes (a session cookie). HttpOnly hides the cookie from JavaScript so a cross-site scripting bug cannot read it. Secure means send only over HTTPS. SameSite controls whether the cookie rides along on requests started by other sites, which is the main defense against CSRF attacks.
A common pattern is the session cookie. The server stores the real user data server-side and the cookie holds only a random opaque session id. The alternative is a signed token, such as a JWT, stored in the cookie, where the data lives in the cookie itself and the server verifies a signature instead of doing a lookup.