There are plenty of reasons why people cheat, but I'd say one big motivation is when grades and test scores decide who wins and loses in a zero sum game.

Medical school is a good example - there are a fixed number of med school slots, and slots at the top schools are more valuable. Law is similar, but more skewed, it's easier to get into a low ranked accredited law school than an accredited med school, but you do still generally need to attend an ABA accredited school, and spots at top schools are far more likely to lead to high paying jobs. Another interesting factor is that attrition rates at top law and med schools are typically below 0.5% (not a typo - yes, that's one half of one percent). As a result, just getting in is critical. No, you can't succeed in med school if your grades and test scores are an artifice and you have no understanding, but I think the whole thing is set up for low attrition rates, so a B student who cheats and ends up A grades will be fine at an elite med school. I think this is also why high achieving students are (no cite, this is what I've read) no less likely to cheat than low students. You might think, why would someone with a 3.7 in pre-med cheat? it's because things are wildly competitive, and the selectivity is actually higher than what is needed to establish basic competence, people are essentially outcompeting each other for limited spots in a cartel.

Interestingly, programmer jobs generally is less like this. If I learn to program, that doesn't mean someone else doesn't get to, if I get a degree in CS, that doesn't mean one of a finite number of spots can no longer go to someone else. I suppose we do all compete with each other as workers in some sense, and top schools do give an advantage, but we don't place a hard cap on the number of CS grads or programmers who can be employed, there's no formal accrediting body that can act as gatekeeper, and there's no credential with high barriers to entry but a vanishingly low attrition rate once you get through. If you cheat on your data structures and algorithms test, google is still going to make you find all matching subtrees in a binary tree at a whiteboard in your interview (I have this sneaking suspicion that the whiteboard exam at google may be an unnecessary exam, and that people who fall a bit short of this would probably be absolutely fine once at the company, so it may be more of a med-school style situation, even if there's no government-backed gatekeeper). If you go the startup route, well then, nobody's going to care one way or the other if you got an A in data structures instead of a B or even a C+ (they might note the degree, I suppose, but even that would be secondary).

If the value of an education is what you've learned, rather than the measurement of learning, then cheating will be far less prevalent. If the measurement is more important, then yeah, there's a huge incentive. And let's face it, platitudes like "you're only cheating yourself" are silly when turning a B into an A means you get a coveted spot on a track to earning $375k on a 40 hour workweek as a dermatologist. People often cheat because it often does work, and often does pay.

Then, of course, there's the extremely insidious situation where some degree of cheating is so common and tolerated that not cheating places you at a severe disadvantage. I read that students at one university rioted when they discovered they were going to e prevented from cheating on an exam. This seems bizarre and appalling (and it is, appalling, in a general sense), but it makes more sense when you realize that a culture that broadly permits cheating, but then clamps down at a subset of test sites, has in fact placed the non-cheating students at an insurmountable disadvantage. I think this may also be the case for some highly competitive sports - I have nothing to do with cycling, but much of what I read during the Lance Armstrong scandal suggested that people who refuse to cheat have essentially conceded and chosen not to be elite pro cyclists. In many ways, to me, what was so damaging about the story (re Armstrong) wasn't the cheating per se (though that's bad), it's how viciously he went after people who reported or questioned his cheating.