As I understand it, the issue is that the official pathway to hire a permanent foreign worker (PERM status) is very long (18 months+), and most companies don't want to start a process in hopes of hiring someone in a year or more. H1B offers a shortcut, where they can be brought in on a temporary permit, then apply for PERM status. But PERM status requires a bona fide search for American workers; using the H1B shortcut legally would require an awkward job search where you already have an employee in the role, and if an applicant is found the current employee not only loses their job but has to literally leave the country. So instead of getting into that awkward situation, employers are faking the "bona fide search" requirement and trying to hand the green card status directly to the H1B even when Americans are available that could do that job.
That said... there is still the question of why companies choose to go down this road instead of simply hiring Americans. We can speculate about their intentions (cost saving via lower wages, employees willing to work more hours and under worse conditions, racism, etc) but it's unlikely that they're violating federal law just for fun. This is a lot of hoops to jump through and risk to take on without a compelling reason to do so.