What hreflang does
Hreflang annotations link equivalent pages in different languages or for different regions, such as English for the US, English for the UK and Spanish. Search engines then show a searcher the version that matches their language and location, instead of treating the versions as duplicates.
You can declare hreflang with <link rel="alternate" hreflang="..."> tags in the head, in HTTP headers, or in the XML sitemap. Google supports all three. Bing relies more on the content-language meta tag and the page's own language.
Syntax rules
The value is an ISO 639-1 language code, optionally followed by an ISO 3166-1 alpha-2 region code: en, en-gb, es-mx. A region on its own, such as gb, is invalid, and en-uk is wrong because the code for the United Kingdom is gb.
Every page must list all versions including itself, and each version must link back (return links). Add hreflang="x-default" for the fallback page shown when no version matches, often a language selector or the international homepage.
Common hreflang errors
Missing return links, wrong codes, pointing to URLs that redirect or return 404, and hreflang URLs that conflict with the canonical tag (each version should canonicalise to itself, not to one master language) are the usual problems. Any of them can make Google ignore the whole set.
Example
Hreflang for three versions:
<link rel="alternate" hreflang="en-us" href="https://example.com/us/">
<link rel="alternate" hreflang="en-gb" href="https://example.com/uk/">
<link rel="alternate" hreflang="es" href="https://example.com/es/">
<link rel="alternate" hreflang="x-default" href="https://example.com/">