Every experienced freelancer has a story about a client who doesn't pay or who is slow to pay. It's going to happen to you too, but there are a few ways to make such experiences less frequent.
Your first line of defense is your contract. You should include clauses that specify not only the amount the client will pay you, but also how and when. Along with the payment clause, have clauses that spell out precisely what the client is getting from you. That lays out your argument before it happens by saying, "I did A, B, and C as the contract specified." Now it's time to hold up your end of the bargain. But clauses are only as good as their enforcement, so it's important you have text in the contract that explains what will happen if you're not paid. It needs to be realistic, actionable and legal; and you have to be prepared to go through with it. For example, a common clause in the United States says that both parties will seek mediation or arbitration before going to court and it spells out the conditions.
You might ultimately decide it's not the worth the trouble to follow through, but again, someone is eventually going to try to get away without paying, so be prepared. Finally, don't start the work until the contract is signed or until you have other legal proof of agreement. This is the part that can be hardest because you'll be eager to get going. But it's important psychologically that the client acknowledge that the project is actually going to happen and that they will have to go through with their responsibilities. There is another way to make sure you get paid, and that's to take payment in advance, either partially or in full. I'm of two minds about this. On the one hand, requiring advance payment could hinder the sales process. You're essentially demanding that they trust you more than you trust them. On the other hand, they are going to have to pay you sometime, why not at the beginning. There is one situation where you should probably get payment upfront. If the job requires you to buy materials that are specific to the project or spend your own money to get it started, make sure the client covers those expenses first. In this area, you're just going to have to develop your own judgement and listen to the experiences of your colleagues.
So you have finished the work and now it's time to collect. As a reminder, you should present your client with an invoice. By the way, some accounting programs have their own invoicing function, including the popular QuickBooks. In any event, an invoice should include at least the following elements. Start with your contact information, then put the word INVOICE in big letters at the top of the page. That will help prevent it from being buried. Include a statement that you expect to be paid, how you should be paid and when the deadline for payment is. By the way, I'll often say something like, "In 45 days, but then I'll include the actual date." Then give a brief description of why you're getting paid. Here I'll often reference the agreement saying for example, In accordance with our contract of February the 15th. And of course, specify the amount you expect to be paid.
Some clients also like you to include an Invoice Number to help them track it in their records. And if it's for hourly work I'll paste my time sheet at the bottom of the page. Finally, send it off, make a note of the due date in your calendar and try to put it out of your mind. You can drive yourself crazy worrying whether someone is going to pay you, but remember until the deadline is passed there's nothing to be done. I found that most clients are pretty good about paying on time, especially after your first project together. Give them the benefit of the doubt, but then be prepared to act once the deadline is passed.