score:4
As you've said, it is not possible to apply for a visa whilst in the US. All visa applications have to occur at US Consulates, and there are no consulates within the US.
It is likely that you will be able to apply for your visa by mail (under special rules that run through until the end of 2022), however one of the requirements for this policy is that you must be physically present in the country where you applying. ie, it's not possible to mail your application from the US to your home country to apply, so again you can not do this whilst you are physically in the US.
It is possible to "change status" whilst inside the US (eg, changing from B-1 status to J-1 status), however it is not possible to change status if you've entered the country under the Visa Waiver Program (VWP, aka ESTA), so that's not an option for you.
The possibility of travelling to a nearby country such as Canada to apply for a visa was mentioned in the comments, however be aware that there is a significant backlog at most US consulates at the moment, and even getting a visa appointment could take months or more. This is especially true for Canada, which has a longer wait period for non-Canadian citizens/residents which can often be 6 months or more (potentially much more at the moment).
The simple answer is that you should talk to the International Students office at the university that you're looking to work at. They will be able to advise on the best course of action, including whether you can commence working whilst visiting on the VWP/ESTA program (very unlikely, although it can depend on the exact situation), as well as the best course of action for obtaining a visa and how to get that process started. They are also the ones that will need to provide the relevant paperwork for the visa, so the sooner you get that process started the sooner you'll be able to obtain a visa.
Whatever you do, do not plan to work (or even enter the US) without the relevant documentation/visa/etc. If you do, and if you are caught, then you will likely be barred from entering the US for a period of time which will affect any future visa applications. It's simply not worth it.