In more detail
It tells search engines which duplicate to rank, such as /page instead of /page?ref=x. The tag goes in the page's <head>; files such as PDFs can send the same hint in an HTTP Link header. Duplicates come from tracking parameters, sort orders, http/https and www/non-www variants, trailing slashes and print versions. Search engines treat the canonical as a strong hint, not a command, and may pick another URL if other signals disagree. A self-referencing canonical on every page is common practice. See How SEO Meta Tags Work.
Why it matters
Without a canonical, ranking signals are split between duplicates and the wrong variant, such as one with a session ID, may appear in results. A wrong canonical is worse: pointing every page at the homepage, or at a URL that redirects or returns 404, can drop pages from results. It must be an absolute URL.
Example
On https://example.com/shoes/?sort=price&ref=newsletter:
<link rel="canonical" href="https://example.com/shoes/">
Search engines index /shoes/ and credit links to the parameter version to it.
Common confusion
A Redirect sends visitors to another URL; a canonical only advises search engines, and visitors stay on the URL they opened.