macOS's firewall feature blocks unwanted network traffic coming into your computer, and stealth mode makes your Mac essentially invisible to hackers snooping for computers to target. They aren't foolproof features, but they will keep most people from finding and attacking your Mac on public networks.
First, you need to make sure your Mac's firewall is enabled:
• Go to Apple menu > System Preferences.
• Choose Security & Privacy.
• Select the Firewall tab.
• If the firewall is active you’ll see a green dot and "Firewall: On." If not, click Turn Firewall On. You may have to click the padlock icon and authenticate with your Mac's password to change the setting.
Next, enable stealth mode:
• Click Firewall Options. It's below the button for turning the firewall on and off.
• Check Enable stealth mode.
• Click OK.
"Automatically allow built-in software to receive incoming connections" and "Automatically allow downloaded signed software to receive incoming connections" should already be checked. Those settings let the apps you already have communicate through the firewall without you having to take any extra steps. Leave those checked unless you know what you're doing and plan to manage app network access manually. You should leave "Block all incoming connections" unchecked too, unless all you're doing is surfing the Web.