If www.mysite.com and mysite.com both show the same page, Google sees two pages. Links to your site get split between them. A 301 redirect fixes that: one version, all the ranking in one place.
Not sure you're on Apache? Ask your host. Cloudflare, cPanel, and Nginx each have their own way of doing this.
Find the .htaccess file
It sits at the root of your site, often in public_html or htdocs. With WordPress, it's next to wp-admin and wp-config.php.
The name starts with a dot, so it's a hidden file. In FileZilla or Transmit, turn on hidden files. If it doesn't exist, create it.
Redirect www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]Redirect non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]Both examples assume your site runs on https.
With WordPress
Put the rules above # BEGIN WordPress, never between the markers. WordPress rewrites everything between # BEGIN and # END when you save permalinks, and your rules would disappear.
Also check that the address in Settings → General uses the same version (with or without www). Otherwise WordPress and Apache will bounce visitors back and forth.
Test it
Open both versions of the address. Both should end on the same URL. To see the 301 itself:
curl -sI https://mysite.com | grep -i location