BERLIN—Germany’s Social Democrats agreed to prolong their unpopular ruling coalition with Chancellor Angela Merkel’s conservatives, ending a period of political uncertainty unprecedented in the country’s recent history.

The vote lifted the last hurdle for Ms. Merkel to start her fourth, and likely last, term as leader of Europe’s largest economy nearly half a year after an inconclusive general election left the country without a clear ruling majority.

...