Any money you contribute from your paycheck is always 100% yours. But company matching funds usually vest over time - typically either 25% or 33% a year, or all at once after three or four years. Once you're fully vested, you can take the entire company match with you when you part ways with your job. If you're not fully vested, you'll get to keep only a portion of the match or maybe none at all. To find out your vesting schedule, check with your company's benefits administrator.
The upshot: It can usually take around three to five years before you own all of your company matching contributions. Leave your job before then, and you'll lose some of that delightful free money - even if you're laid off.