There is no legally enforceable reason unless there is a product placement deal
There are, as the previous answer shows, many examples where even Apple products are used by "the bad guy" in a movie. This provides pretty good evidence that there isn't any universal legal rule that would allow Apple (or anyone else) to prohibit certain uses of a product in a work of fiction.
The possible exception, as pointed out by iandokelly in a comment, is if the mention of the product is potentially defamatory. But this has to be a rare event as legal standards for proving defamation are relatively high. And unlikely to be met in the case the villain in a movie happens to make normal use of a product. But, big movie firms may exercise an overabundance of caution in many cases to avoid even the small potential risk of being sued by a wealthy product owner (like Apple). This could easily incur a lot of legal costs even if the studio won.
But this doesn't explain the obvious exceptions or the perception that the rule exists.
What seem more likely is product placement.
Product placement has been ubiquitous since the early days of movies and television (though some countries like the UK prohibited it in TV until recently). It is fairly hard to work out how much money is involved in general (though we do know that the tobacco industry paid a lot for it). But it clearly happens a lot.
Even if a product company doesn't explicitly pay for product placement, they may provide free product samples (eg Apple Laptops) to the production which reduces the cost of making the movie. But, if the studio accepts such promotional gifts (or any extra cash) they will have to abide by the rules set by the provider (there will probably be a contract). So, if Apple provides free iPhones and Laptops to the production, they will be able to constrain how they are used.
So, given the prevalence of product placement, many productions will have to abide by the rules set by Apple or other product owners. Hence the common belief that "bad guys can't use Apple products" even though it is not an actual general rule. But this also explains the exceptions, since Apple can't stop normal use of its products if the production didn't get them as part of a deal.