How does a hacker know how many times a password was hashed? The same way you do.
The goal of hashing a password is to make it impossible (in practice very difficult) to determine the password, even with full access to all the data.
The other requirement for hashing is that the server must be able to determine if an entered password is correct. This means that at some point during the login process, the server must have access to the complete formula for hashing a password.
The purpose of varying the number of hashes is primarily to slow down brute force attacks and increase the search space for rainbow tables, and this works without any secrecy. That's why the number of rounds is typically stored right in the database with the hash, as in SilverlightFox's answer.
There are solutions that store the number of rounds separately and attempt to keep it secret, and that may add an extra layer of protection. But when evaluating the hashing system we have to assume that the attacker knows everything we know.
After all, if there were a place to store the number of rounds that was guaranteed not to be compromised, we could just store the passwords there and not bother with hashing at all.