If a sailing boat is on passage in international waters near US territory, can it anchor overnight within territorial waters for eg shelter/rest/repairs or whatever if nobody goes ashore?
I believe the issue is not about whether people ("persons") on the vessel have visas or not. Whether or not they have visas is completely irrelevant to anything except when going through customs to see if they'll let you in. All cargo, the vessel itself, and people on board, even if they have US passports, must check in to customs.
Vessels that have not cleared customs usually follow strict rules. It varies by country. Vessels are allowed to pass through seas belonging to another country to reduce their travel distance, or wait in a harbor if required. When entering foreign waters, and before clearing customs, the vessel usually flies the yellow quarantine Q flag. It should proceed directly to a port of entry to clear customs, where only the captain will go on shore. If you have passengers who cannot clear customs, then your whole boat will probably be restricted and those passengers will not be permitted to go on land (unless you hand them over to customs as stowaways). After checking in, you should still be able load and unload other people and cargo and buy supplies even if your boat doesn't clear. Customs should tell you what you're allowed to do.
So your question is about if a boat that has not cleared customs can anchor in a foreign country's waters. Well the answer depends on the country. For the US the answer is no except for emergencies or as needed to check in.
For the USA, disregarding emergencies:
Until you have cleared customs, it is not legal to anchor, tie up to a dock or tie to a buoy unless you are there to check in. Other then docking assistance, only the vessel captain may leave the vessel until check-in is completed.
This site: http://boating.ncf.ca/usborder.html says mostly the same thing.
Once the craft enters US territorial waters, for any reason, US Customs and Border Protection must be notified immediately and the vessel is subject to inspection, along with all aboard. Both it and its passengers have to comply with US laws, including immigration, and those who require prior approval before travel to the United States (ESTA, visa) must have done so.
Pursuant to 19 CFR 4.2, operators of small pleasure vessels, arriving in the United States from a foreign port or place to include any vessel which has visited a hovering vessel or received merchandise outside the territorial sea, are required to report their arrival to CBP immediately (see 19 U.S.C. 1433).
The master of the vessel reports their arrival at the nearest Customs facility or such other place as the Secretary may prescribe by regulations. These reports are tracked in the Pleasure Boat Reporting System. Pursuant to 8 CFR 235.1, an application to lawfully enter the United States must be made in person to a CBP officer at a U.S. port-of-entry when the port is open for inspection.
CBP has designated specific reporting locations within the Field Offices that are staffed during boating season for pleasure boats to report their arrival and be inspected by CBP. The master of the boat must report to CBP telephonically and be directed to the nearest Port of Entry to satisfy the face-to-face requirement, or report to the nearest designated reporting location along with the boat's passengers for inspection.
Exceptions to Face-to-Face reporting to CBP
Alternative Inspection Systems (AIS) satisfy the boat operator's legal requirement to report for face-to-face inspection in accordance with 8 CFR 235.1, but boaters must still phone in their arrival to satisfy 19 USC 1433.
There are four exceptions to the face-to-face inspection at a designated reporting location, NEXUS, Canadian Border Boat Landing Permit (I-68), Outlying Area Reporting Stations (OARS), and the Small Vessel Reporting System (SVRS). Participation in any of the programs does not preclude the requirement for physical report upon request by U.S. Customs and Border Protection.
Any small pleasure vessel leaving a United States port into international or foreign waters, without a call at a foreign port, does not satisfy the foreign departure requirement. Therefore, certain fishing vessels, cruises to nowhere, or any vessel that leaves from a United States port and returns without calling a foreign port or place, has not departed the United States
Law depends on circumstances. OP specifies yacht and "sailing boat" & "overnight". Under those circumstances this is common practice and is part of "innocent passage".
A sailing craft anchoring overnight 10 miles off the coast is not the same as a guided missle cruiser sailing into Boston Bay without permission.
Even pulling into a confined harbor without everyone having visas wouldn't necessarily land you in hot water. Allowances are made for circumstances. The laws & customs of the sea place a very high priority of safety, and by and large, they are pretty much common sense.
Are you subject to U.S. law within U.S. territorial waters? Sure, BY DEFINITION, because that is what "territorial waters" MEANS. Does everybody on board have to have a visa or equivalent to anchor overnight. Absolutely not.
Of the above, I'm certain. I'll go further, but without certainty. Even on cruise ships coming into port, I don't think people who don't leave the ship are legally required to have a visa. Also foreign Naval vessels on state visits (or for emergencies) - I'm fairly confident the same applies.
According to the United Nations Convention on the Law of the Sea, Part II, Section 3:
Right of innocent passage
Subject to this Convention, ships of all States, whether coastal or land-locked, enjoy the right of innocent passage through the territorial sea.
Meaning of passage
- Passage means navigation through the territorial sea for the purpose of:
(a) traversing that sea without entering internal waters or calling at a roadstead or port facility outside internal waters; or
(b) proceeding to or from internal waters or a call at such roadstead or port facility.
- Passage shall be continuous and expeditious. However, passage includes stopping and anchoring, but only in so far as the same are incidental to ordinary navigation or are rendered necessary by force majeure or distress or for the purpose of rendering assistance to persons, ships or aircraft in danger or distress.
Meaning of innocent passage
Passage is innocent so long as it is not prejudicial to the peace, good order or security of the coastal State. Such passage shall take place in conformity with this Convention and with other rules of international law.
Passage of a foreign ship shall be considered to be prejudicial to the peace, good order or security of the coastal State if in the territorial sea it engages in any of the following activities:
(a) any threat or use of force against the sovereignty, territorial integrity or political independence of the coastal State, or in any other manner in violation of the principles of international law embodied in the Charter of the United Nations;
(b) any exercise or practice with weapons of any kind;
(c) any act aimed at collecting information to the prejudice of the defence or security of the coastal State;
(d) any act of propaganda aimed at affecting the defence or security of the coastal State;
(e) the launching, landing or taking on board of any aircraft;
(f) the launching, landing or taking on board of any military device;
(g) the loading or unloading of any commodity, currency or person contrary to the customs, fiscal, immigration or sanitary laws and regulations of the coastal State;
(h) any act of wilful and serious pollution contrary to this Convention;
(i) any fishing activities;
(j) the carrying out of research or survey activities;
(k) any act aimed at interfering with any systems of communication or any other facilities or installations of the coastal State;
(l) any other activity not having a direct bearing on passage.
The US has signed, but is not party to the United Nations Convention on the Law of the Sea. That said, the US Navy has two main functions: force projection, and ensuring innocent passage and freedom of navigation, two related, but distinct parts of international maritime law. The US may not be party to the UNCLOS, but it considers it an important part of international law, and it is indeed important to America's global hegemony; the US military has expressed its strong support of the US being party to UNCLOS(US Senate Press Release). "The U.S. is not a party to the LOSC, but it considers the provisions of the Convention relating to the high seas and navigation rights to be a reflection of the customary international law that is binding on all States" (tufts.edu/lawofthesea). According to the US Navy (PDF)), the US has no restrictions on innocent passage, whereas you can see the Chinese restrictions here (PDF). A full link to the US Navy list can be found here. The US does not only operationally challenge countries like China; according the the FY2020 Freedom of Navigation Report (PDF), they challenged relatively neutral countries like Brazil or Algeria, as well as allies, such as Japan, South Korea, and Taiwan.
It can be asserted that the US upholds the UNCLOS as a matter of principle (regardless of whether or not the USG has any ulterior motives).