In many jurisdictions1, an employer is required to provide advance notice to employees who are losing their jobs involuntarily, unless they are being terminated for cause2. The ostensible purpose of such notice is to give the affected employee(s) time to find other employment. In many cases, employers will want the employee to leave right away, and will offer pay in lieu of notice (also called termination pay) as part of a severance package. However, the employer (but not the employee) has the option to require the employee to continue to work during the notice period.
A short period of working notice can provide benefits to both employer and employee. The employer gets a chance to have the employee wrap up work, transition tasks, and transfer knowledge. The employee gets a chance to make peace with their departure, clean up any personal files, and to say farewell to friends. The employee may also find comfort in initiating their job search while still employed. In the case of large termination events, working notice can provide an economic advantage to the employer in that some of the notice costs can be recouped with actual work performed.
If the termination is reasonably amicable, and the employer does not fear that the employee will commit sabotage, destroy morale, or copy company secrets, a two-week notice period is reasonably common. Most work can be wrapped up or handed off in that time-frame. Longer notice periods may be mandated by law, but the remainder is usually handled by payment in lieu of notice. Two weeks of "I see dead people" in the workplace is as much as most people can stand.
An employee in the working notice period can choose to resign during that period, in accordance with their employment agreement, and still receive their severance pay. As employment agreements typically require two-weeks’ notice of resignation, an employee on a two-week working notice will not benefit from this option.
For termination without cause, there may be a separate, statutory severance period which the employer cannot require the employee to work.
My employer did not always give working notice. During one large termination event employees were walked out immediately. This was stressful on the 'executing' managers and the human resources team, as it required extra security and a large amount of skullduggery to ‘pick off’ the affected employees without alerting their peers. Afterwards, we reverted to the two-weeks notice policy.
Some employees react badly to working notice. Technically refusing to work is job abandonment, but pursuing that opens a legal nightmare for all parties. I have had employees react poorly, and in such cases we informally agreed to an "I'm working from home" facade that included an expectation to participate in transition tasks. In one case the participation in this activity was so toxic as to preclude further attempts. That employee burned their bridges on the way out, but perhaps they achieved a measure of satisfaction from it. But most people are professional about it, and understand that the decisions are driven by economics, and are not personal. Also, people like to leave on good terms in order to encourage positive references, or to keep the door open to future working relationships.
I found my own two-week notice period to be productive. I was able to blow off meetings (including the one where my own termination was announced). I was able to ignore all manner of communication that would otherwise have necessitated pausing my tasks. I was even able to perform an important but low-priority project that had been hanging over my team like a Sword of Damocles for weeks. By the end of the second week I was at relative peace, and I felt like I was ready to leave on good terms. (It also helped that in my jurisdiction, legal requirements and company policy combined to provide a generous severance package.) So for me, working notice was a positive element of an unfortunate life experience.
1 In jurisdictions with "at-will employment", these concepts do not apply.
2 Such employees ought to be well aware of imminent doom via HR-mandated performance improvement plans or similar jolly actions.