If you leave Germany for a longer period (e.g. returning to your home country), you have to sign off formally with the responsible registration office (Einwohnermeldeamt) where you registered when you first arrived.You can download the form here
Do not forget to bring along your own passport as well as the passports of your family members.
If your residence permit has expired and you are about to return to your home country, you will have to get in contact with the foreigners’ registration office. Upon showing your passport and your plane ticket, you will get a so called “Border Crossing Certificate” (Grenzübertrittsbescheinigung, GÜB). A date will be given on the border crossing certificate until when you have to leave Germany at the latest. You have to hand in the border crossing certificate when crossing the border or at a German consulate in your home country. They will send it back to the respective foreigners’ registration office in Germany.
With the border crossing certificate, you prove to the Federal Border Guard that your stay in Germany is legal even if your residence permit has expired.