The best way to avoid copyright infringement is to ensure that you don’t use anything created by anyone else.
If you do use someone else's work, make sure you have the necessary permissions – this will usually take the form of a license from the copyright owner(s), which you may have to pay for.
There are certain instances where you may be able to use excerpts of copyrighted material without a licence – for example, if you use a small part of someone else's work for the purposes of criticism or review, or if your use constitutes "fair use" under applicable law (particularly U.S. law) – however, discussion of these exceptions is beyond the scope of this guide.
If you intend to use any part of a copyrighted work in reliance on any of the statutory exceptions, you should seek legal advice first.
To read more on copyright and other related articles:
Do I own the copyright to my own project?
Can I record my own lyrics over someone's beat that I've downloaded?