The company needs a fair reason and must give 30 days' notice, or pay 30 days of salary (解雇予告手当).
When the company ends your contract.
Ask for the reason in writing (解雇理由証明書). If you were fired, do not sign a resignation letter: it changes your unemployment benefits.
General explanation, for guidance. Rules and amounts change: confirm with the responsible office.