What is a security deposit on a apartment?

Let's say I find a place that is 800 a month for rent. they require first and last month rent then a security deposit. What is that for? if I was to move out one day I would have the last month covered right? would they refund me security deposit? I'm so sorry for the dumb question I have never had my own place to live but I will be searching soon.