In System Preferences > Security & Privacy > General, make sure you've checked "Require password [x seconds] after sleep or screen saver begins":

In Sonoma (or earlier, perhaps, I can't remember) and later, the functionality has been moved to System Settings > Lock Screen > Require password...:

