How do I close a port on a Mac?
If all you need is a firewall, that is well documented how to turn it on and make exceptions to the default rules.

https://support.apple.com/en-us/HT201642

In the advanced settings there is a “block all incoming connections” toggle for that specific simple configuration.