How do you close an open port in Mac?
Ports on macos only open when a process requests to listen on that port. 

First order of business is to list the process tree and hope one single running process has that port open. 

sudo lsof -i:9999


Should that fail, now you have a special helper process that will register non-running processes that get woken or spawned when packets come in for them. This process is launchd Think of it like a doorman that lets the system idle until a process is needed to handle something over its registered ports. 

Since your question has about five sub questions, let’s leave this small answer to a very large question and perhaps you can ask a follow up question detailing how to learn launchd and where it stores it’s configurations. That alone would be a couple times larger than your original post. As is a summary of firewall operations. Each of these depend on specific MacOS versions for the details that are relevant. 

To summarize, you close a TCP or UDP port by identifying the process that opened it and stopping that process or changing the configuration file that tells launchd to listen as a proxy.