Germany's two main parties have finally struck a deal to form a coalition, more than four months after the election.