Like you've mentioned, the hosting and the domain are two completely separate things.
In your case, Company B doesn't care. It just provides the storage and the environment. When you point your domain to your hosting (by changing the name servers), you need to go to your hosting configuration (most often in the form of a control panel such as CPanel) and assign the domain to point to a folder in your hosting account. That's it.
You do raise a good point though - what if you're using a shared hosting and there are other people on the same server? Can someone maybe hijack a subdomain of yours that you haven't yet assigned to your account?
Sometimes the answer is yes. This can happen if you create a wildcard
A record for your domain so that
*.yourdomain.tld points to your host's IP address. Now it's up to the control panel to allow or forbid that someone else to perform that action - you can't be certain that the panel will tell them "Uh-oh, looks like you're trying to point another client's domain to your folder".
And let's look at one final case. Let's assume that you and I are used the same shared hosting provider and somehow ended up on the same server. I used to own
mydomain.com, and it's pointed to a folder under my hosting account. But it has expired and you bought it, and now you want it to point to your folder. That's when an unexpected result can occur (meaning it could point to my folder again while you're the owner of the domain), but you can always poke the support, and I'm sure they would assist you. And the chances for that are extremely slim, as you can imagine.