If your Joomla site is accessible both with AND without a www-prefix you coud have an issue with duplicate URL's. Google could see both versions of your pages as multiple instances of the same content and penalize your site for it. Make sure you choose one of those and make sure the other one is not accessible at all. Which one you choose does not matter for Google, but if Google does not care, why bother to type the www every time. Unless there are specific reasons, choose the non-www version. By the way, there are more situations where this issue can arise (http / https, lowercase, uppercase, etcetera), but the www issue is the most frequent issue.

After you made your choice for www or non-www, configure your site to be accessible through only the chosen version, and set up a redirect from the non-preferred version to the preferred one. If you already have a SEF extension on your site, it may have an option to do so. If not, it is very easy to set up the redirect in your .htaccess file. Using .htaccess is the preferred option anyhow, as it is the fastest. Here is the code for our two scenarios:

Enforce non-www redirect

RewriteEngine On
RewriteCond %{HTTP_HOST} www.site.com
RewriteRule (.*) http://site.com/$1 [R=301,L]

Enforce www redirect

RewriteEngine On
RewriteCond %{HTTP_HOST} ^site.com [NC]
RewriteRule ^(.*)$ http://www.site.com/$1 [L,R=301]

After the change, always check if the redirect works as desired, or even that the site still works. A minimal typo could already result in a blank page or server error (500 code)... If this happens, just undo your change, or restore the .htaccess version distributed with new Joomla installs. By the way, if you use Akeeba's AdminTools, you can configure the redirect within the .htaccess maker.

Specify preferred domain in Google Webmastertools

Secondly, make sure to go into Google Search Console and specify how you want Google to index your site, with or without www. Even if you already performed the previous step, still do so. It's a very simple step in the configuration: Click the configuration icon in your Webmaster configuration and click Site Settings. Then you can set the preferred domain.

SSL versions of your site

Note that when you install an SSL certificate, you could have the same issue as well: http:// and https:// also create duplicate versions of your pages. Also here, enforce the preferred version (obviously, use the https:// version). You can do so in Joomla's Global Configuration settings, Server tab. Also register the https versions of your site in Search Console then. This process is described in great detail in my blogpost about SSL for Joomla.

Advanced tweaking

A while ago I had issues with one of my sites, where for some pages the IP-address version of the site was indexed by Google: a clear duplicate content issue Most sites don't suffer from this problem (so only apply this fix if you spot issues!), but in this case I needed to solve it. I redirected all traffic directed over the IP-address of my server to the actual site, using a variation of the code above, where XXX.XXX.XXX.XXX needs to be replaced by the IP-address:

RewriteCond %{HTTP_HOST} ^XXX\.XXX\.XXX\.XXX
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]

Sometimes you can even have other variations of your domain. Just make sure to monitor your SEO status to keep track of this. You can use Ryte.com or SEO Powersuite's Website Auditor for this.