To make it simple: there is absolutely no way to ensure you will get paid.
Even if you have a contract there is no way to be assured, that you will get paid.
Tons of companies went bankrupt, because contracts weren't fulfilled.
Even if you work for a company with reputation, or an individual with a "big name" will ensure you that you will get paid, because they already could be bankrupt or whatever.
The best way to ensure safety is risk minimization.
You can minimize risk by:
Research your Business Partner:
It's as simple as putting his name in google, on facebook and wherever, and looking through the results which show off. If there are multiple reports of fraud, it surely is risky. If there is one report or so, it's a possibility to show/ask your business partner about that (sometimes stuff pops up which might be very one-sided)
Half the Risk:
Your risk is time investment. your business partner's risk is "money investment".
For small business relationships 50% of the money in advance and 50% of the money afte rcompletion is the fairest way for both to deal with it.
Seperate the Work in Chunks:
If the work reaches a certain workload, some milestones need to get set. An example: a 10000$ job. Loosing 5000$ due to a fraud, is quite the risk for the employer, because it's a big chunk of money. But you can divide it in smaller portions too, which is the best compromise between safety and amount of transactions.
Make sure that you can back out anytime, if the emloyer doesn't take care of a partial payment though.
Set up a Written Agreement:
A contract basically is just a written agreement between both parties. Make sure to nail down the amount of payment, the time until the work/payment is done and the conditions for both parties to back out of the deal. Keep the written statements equal for both parties. Also make sure that the court of law is your court of law if your partner breaks the contract, and his court, if you break the contract.
If you don't think you can cover all eventualities yourself, go to a lawyer who can educate you on contracting and set up a basic contract with you. For certain jobs which are too big and risky you even want to set up the contract specifically for that job with a lawyer, but that's usually "company-level" only and doesn't apply in most cases to basic freelancing.
A contract can help in certain occassions, but is not the "magical potion" which will cure every problem.
Don't get baited by profit-share Profit share, or getting a certain percentage of a product if the product gets finalized might sound very attractive.
THe truth is that most game projects end as "vaporware" because game development is highly complex and new tasks show up as the development progresses.
So even if you did all the graphical work, but the dev has problems with a library, or with getting the game for sale in a shop, or if he just stops his own work, will lead to zero of the expected earnings for you.
And even if the product gets released you get what percentage/share of what exactly? Have you set all terms really clearly? Does your business partner have any understanding of taxes / shares etc.?
And even then he still could betray you and don't pay you anything - I won't expect that, but it happened.
I'd do profit share only with individuals you know that they will get "their shit done" (you know or worked with that person for a long time) and that they are in your physical reach. It's just to easy for someone to go black in the net.
Working with a friend you know since childhood and you did already some stuff together and you want to start a company: lowest possible risk, but still fairly risky
Working with a random person who just approached you via mail, has some company, no released products and you don#t know them, but they offer you a "profit share": wouldn't even consider touching something like this, because the possible risks outweigh the possible gains by too much.
I don't say: don't do it on any occassion. I just say, don't let that be the "bait" which will make you do the work. Always think about possible "failing-scenarios" really carefully.