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 and black quarantine 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.
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.
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