The Coast Guard was part of the Department of TRANSPORTATION prior to being folded into DHS.
They have the authority to inspect any vessel in US Teritorial waters for safety compliance, proper cargo manifests, compliance with HazMat regs, etc.
Here's the concept that you need to understand.....all ship traffic is highly regulated. You don't just write a check to buy a ship and cast off from the dock hauling cargo. Different countries have varying requirements for safety, maintenance standards, crew certifications & training, and most importantly INSURANCE COVERAGE. Liberia is one of the most "flexible" and that's why you see so many merchant ships "flagged" aka "registered" there. The fees required also vary greatly.
Here's a parallel for you...trucking in the US. I own/operate an 18 wheeler under my own DOT authority. I'm subject to being pulled-over and inspected by any state's DOT at any time. They can, and frequently do, examine my DL, Medical Card, insurance cert, logbook, cargo manifest, and appropriate registrations. They can perform a detailed mechanical inspection any time they want to. If they find a violation they can basically impound (issue an "Out of Service" order) the truck until repairs are made. Moving a truck that has been tagged out of service is BAD Juju! I have to register my truck in every state that I run in. The DOT cops (or practically any cop) can search my truck/sleeper from stem to stern (upheld by SCOTUS) PLUS require me to provide a urine specimin any time they want to. Since I'm also a "trucking company", I'm subject to an audit of all required records at my place of business by my state's DOT. Personnel records, training records, drug testing program, safety program, maintenance records/program, tax compliance, regulatory compliance...the list goes on. The fines can be astronomical if the inspector is in the mood to "stick it to you".
There are enough books on maritime law and international trade regulations to fill a library all their own and governments take this stuff VERY seriously.