There are many potential reasons for a company to discriminate based upon your country.
Taxes can get very complex, very quickly. Even if your country allows you to take on all of the tax responsibilities, which is relatively uncommon, the company will have to spend money even getting an expert on your country's laws to confirm this. Otherwise, the company will have to comply with tax laws of the US, the state the company is in, and your country. In some cases, it might even cost the company more money to comply with the tax laws of your parent country than your salary. It is not even that uncommon for smaller US companies hiring remote workers that must be based out of only one state, because it is costly to even comply with multiple US state tax and benefit rules.
This also raises issues with tax incentives that might be offered by the either the US federal or state governments. Some locations might provide tax breaks for employing local citizens.
Labor laws vary even between different states, and vary drastically between different countries. There is a lot of cost involved with even identifying all of company's responsibilities to you—let alone complying with all of the applicable labor laws. How many days off a year do I have to give you? If you have a child, do I lose you as an employee for 6 months, but still have to pay you? What am I allowed to do with your employee data, and how am I required to store it?
Technology & Export Laws
There are laws that restrict the flow of technology from the US. Take for example, encryption export laws—although these have been greatly relaxed in recent years, there are still restrictions on what encryption algorithms are allowed to be used in products sold to a non-US country. This also leaves the potential for gray areas—it can be difficult to determine the legality of having someone who lives in country A working on technology X. In some cases, this could either be very expensive to figure out & keep track of, or the law might be ambiguous enough that it is easier to simply not have to worry about this becoming an issue at all.
Intellectual Property Concerns
Countries tend to have different laws dealing with IP, and some countries are known to ignore IP claims of other countries if doing so has the potential to benefit them (as an example, look at claims of IP theft in China for the last few years). It is potentially easier to protect your company's IP if both you and your employees are following the same rules. Also, if an employee steals a company's IP in the US, there is a clear path for the company to take legal action against that employee.
Due to how many jobs have left the US to go overseas, it can be useful for marketing if companies are able to say that their products are fully made in the US, or that they only employ US citizens.
Within the US, there are multiple levels of government, all of which have their own rules on who they allow to bid on and win contracts for government work. Government work is usually one of the most stable, and sometimes the most lucrative, sources of revenue for a lot of these companies. For example, if your US company has foreign-based employees, and is fulfilling a contract for the US Department of Defense, your US-based employees might not even be able to talk to non-US-based employees without extensively documenting every time they have a conversation. There might also be state or lower governments who require or give preference to companies who are employing people who work in their districts (in an attempt to keep taxes and jobs as local as possible).
Companies will generally want to vet that you are who you say you are, that you are not a criminal, and that you do not partake in illicit drugs. They will usually already have a partnership or contract with a company who can perform background checks on you. In the US, it is not uncommon for even retail style jobs to require you to undergo a background check and drug screening. If you are not US-based, the company might not be able to get a reliable, accurate background check done, or might not want to spend the time and money it will cost to find a company in your location to complete it.
This also concerns cyber security. Depending upon the security controls for a given company, they might not feel comfortable allowing VPN access to their internal network from a foreign country, or they might not want to face increased risk of the data you might have stored becoming compromised.
The US is a large country, and has a lot of highly skilled individuals. There are not too many remote jobs where it would not be possible to find a US worker who is able to complete that work. Given that it almost always possible to find a US-based worker to fulfill a role, and all of the potential pitfalls listed above when hiring a foreign worker, it makes sense that non-international US-based companies would primarily hire US-based workers.