Sitemap generation in Astro starts with the build process collecting all site routes like '/', '/about', and '/blog'. These routes are formatted into XML entries inside a <urlset> tag. Then, a sitemap.xml file is created with this XML content and saved to the output folder. This file helps search engines discover all pages on the site. The process completes when the sitemap.xml is successfully saved. Variables like 'routes' hold the list of URLs, 'xmlContent' holds the XML string, and 'sitemapFile' represents the file created and saved. Key moments include understanding why all routes must be collected first, recognizing the XML format used, and knowing when the file is created and saved.