Hi and Welcome. My Name is ***** ***** I look forward to assisting you today with your question.
On your Mac, go to System Preferences and select Bluetooth. Choose Open Bluetooth Preferences. Now press and hold down the Power button on the keyboard for a few seconds until starts blinking. When it is blinking, it will detect the keyboard. When your keyboard appears in the Bluetooth Preference Pane, click Pair. Follow the onscreen instructions to pair.
(If this is Magic keyboard, then connect the keyboard to your Mac with the lightning USB cable and follow the instructions to connect)
If it still not connecting, then follow the steps below;
Shut down the computer.
Unplug the power cord and also disconnect all other devices.
Wait 20 seconds seconds.
Attach the power cord.
Wait five seconds, then press the power button to turn on the computer. Then pair the keyboard.
If that didn't work, then reset the PRAM and NVRAM of your Mac. Follow the steps below;
Shut down your Mac
Restart and immediately hold down Command-Option-P-R keys at the same time
Hold the keys down until the computer restarts and you hear the startup sound for two or three times.
Release the keys and let the Mac bootup. Pair the keyboard.
Please let me know if that didn't work, so we can continue troubleshooting.