A robots.txt file tells well-behaved crawlers which parts of your site they're welcome to fetch. It isn't a security mechanism — it's a request that legitimate crawlers (Google, Bing, and most other search engines) honor, listed as one or more User-agent blocks, each followed by Disallow and optional Allow rules, plus an optional pointer to your Sitemap.
An example robots.txt
This file lets every crawler in, keeps them out of a private area and the internal search results, and points to the sitemap:
User-agent: *
Disallow: /admin/
Disallow: /search
Allow: /
Sitemap: https://example.com/sitemap.xmlHow the rules are matched
A crawler follows the group whose User-agent name matches it most specifically and ignores the rest. Inside that group, Google applies the longest matching path, and if an Allow and a Disallow tie, the Allow wins. Paths are case-sensitive and match from the start of the URL, so Disallow: /blog also blocks /blog-archive; add a trailing slash to block only the folder. The * wildcard matches any run of characters and $ marks the end of a URL, which is how you block a file type such as Disallow: /*.pdf$.
Mistakes to avoid
Do not use robots.txt to hide private pages: the file is public, so it lists exactly the paths you wanted to keep quiet. Do not block CSS or JavaScript that a page needs to render, because Google then sees a broken page. And do not combine Disallow with a noindex tag on the same page: the crawler never fetches the page, so it never sees the tag. The 6 robots.txt mistakes that can quietly deindex your site walks through the worst cases, and Blocked by robots.txt in Search Console explains the matching status.