A canonical tag is used to designate the preferred URL of a web page, telling search engines - this is the master page, show this in search results.
By defining the canonical tag on a page, you are essentially preventing search engine crawlers from indexing URLs with search queries, pagination, and other parameters.
Canonical tags are defined in the <head> section of the page using <link rel-canonical with href containing preferred URL.
If you don't define the canonical tag, search engines will index every possible version of the URL it comes across.
The way we identify unique pages is quite different from crawlers, they treat everything as absolute. To the crawler, without the canonical URL, each of these parameterized URLs is a unique page with duplicate content amongst each other.
Now that we have covered the basics, let's make a quick distinction between canonical tags and 301 redirects (a lot of people get confused between the two).
A canonical tag is for pages both users and crawlers can navigate to, whereas a 301 redirect is implemented when you want the user to land on an alternate page. Both methods pass link equity (although Google has not clarified to what degree). But if you had to choose between a adding a canonical tag and redirect, always redirect. However keep in mind that both of these serve different purposes.
Example - Let's say you delete the product page of a discontinued item which will never be stocked again. There's no point setting a canonical tag on this because you don't want users landing on this page, it no longer serves a purpose. Instead, you should setup a 301 redirect to a similar product.
Then there's self referencing canonical tags(google loves them).
Self referencing canonical tags point to themselves, it sends a clear signal to Google about the cleanest version of the URL you want indexed; the preferred version of the page whether http or https, www or non www, or with any appended parameters.
You can use Google Search Console's URL inspection tool to find out which page Google considers canonical.
Don't Piss Off Google Bot
When you're dealing with a lot of pages, it easy to get things messed up and send mixed signals to Google over the course of time. Let's say you define the canonical tag on a page and then weeks later redirect that page somewhere else OR you set a noindex on that page OR you add that URL to your robots.txt file - this confuses Google bot, you don't wanna do that.
Any of these mishaps won't fare well for your rankings. If you're sending a signal to Google, makes sure it's a clear one and consistently audit your website to discover such oversights.