Some of the answers touch on the same points I'm about to make, but I'd argue that companies on the whole hire "consultants" almost entirely to avoid taxes on the one hand, and shirk responsibilities on the other.
Many jurisdictions treat self-employed contractors more generously in terms of taxation, and this encourages firms to reclassify employees as contractors and share the proceeds of the avoided tax between themselves.
The second front is shirked responsibilities (other than taxation, of course). Some of these responsibilities are indisputable, such as the need for an employer to plan their workforce needs and to reproduce over the long-term the capacities and skills they and their sector needs.
Hiring contractors allows the hirer to free-ride on the investments that other companies make into workers in this regard.
Equally the hirer could (and do) do this by poaching workers who are then hired as employees too, but then they would have to pay a premium to dislodge employees from an existing stable employer, to whom a long-serving employee may also feel genuine loyalty and dedication.
The contractor however has already jettisoned the employer which invested in their early professional development, and are already footloose and habitually unattached, and thus hiring them demands no such dislodgement premium. They are the mercenaries of their sector. The risk to the employer of hiring such a contractor who really is a rotter, since that might seem like a real risk of fishing in this pool, is mitigated significantly by their lack of job security.
Then there is the issue of responsibilities like taking care of workers' physical and mental health. When employers wear out employees prematurely through overwork or bad culture, or as workers deteriorate somewhat with age, the employer must typically take responsibility for the costs, or for the adaptations necessary to maintain productivity. Often the state has previously acted to impose this responsibility on employers of employees, in order to encourage responsible behaviour by those employers.
When hiring contractors however, the hirer can immediately shirk such costs as they encounter them, and throw the problem they have created onto the hands of the state (and in turn, onto the contributions which other employers make to general taxation, not just saving the shirker money but ultimately increasing the cost of taxation for the responsible employers who are their competitors).
In my view, these are broadly the true reasons for the widespread existence of a contracting model, the scale of which far exceeds any systemic need for flexibility at the margins, and why some employers (whose management have been able to fathom this model of operating, and are quite unconcerned with the moral questions it raises, or any long-term sustainability concerns) explicitly prefer this model.
A particular proliferation of IT contracting in the 90s and 00s here in the UK, was almost exclusively caused by a shift in judicial ideology which showed an increased willingness to deem a professional worker to be a contractor so long as the employer said they were, and thus unleashed a massive relabelling of IT employees as low-tax contractors.