In Next.js, middleware runs on every incoming request. It checks if the request has an authentication token in its cookies. If the token is missing, the middleware redirects the user to the login page, stopping the request from continuing. If the token is present, the middleware allows the request to proceed to the requested page. This process helps protect pages by ensuring only authenticated users can access them. The key steps are reading the token, deciding based on its presence, and responding with either a redirect or continuation.