Turn it off and on again
If you can’t get online using Wi-Fi, turn it off (using its menu bar icon or System Preferences’ Network pane), then on again. For Ethernet, select it in the Network pane, switch ‘Configure IPv4’ from ‘Using DHCP’ to ‘Off,’ click Apply, then switch back, or unplug and reconnect the cable.
If the problem persists, and you’re using your own router, turn it off, wait a minute and turn it on. If necessary, shutting down the Mac while the router restarts, or after, may make a difference.