How do service workers enable offline functionality and boost performance in PWAs?

From what I know, service workers are key to making PWAs work offline and improving performance, but how exactly do they function? How do they manage caching and background tasks to keep the app running smoothly? If you’ve implemented service workers, what benefits have you seen, especially in terms of offline access and load speed?