You mean to password lock it? Go to System Preferences > Security and there you should have an option so that the Mac will require a password after the number of time you've set.
That's the number of time after it went to sleep (or just the screen) or after the screensaver was activated.
If you want it to run in the background you need to go to the "Energy Saver" menu (also in System Prefs) and set the computer to never go to sleep, but only the screen.
I have set it in a way that after 15 minutes the screen would go to sleep. I have no screensaver so the screen would become all black but the Mac can keep working in the background. This isn't needed for downloads or such where the Mac stays awake itself until the download is finished, although since ML I think this feature doesn't work properly anymore. And then of course if I want to wake up the screen of the Mac, it would require a password.
Hope this helped!