Alonso left McLaren because he not only knew of the cheating, but was in possession of Ferrari information, used it to blackmail his boss, and after securing immunity for himself, testified against his own team in court. I genuinely don't know how he kept his drive, so I can only assume it was a condition of his immunity.
He kept his drive because of sponsorship agreements that McLaren couldn't just cancel during the season.
The immunity granted to him (and Hamilton) was with regard to the FIA investigation and penalties. This is almost meaningless and certainly has no bearing on how McLaren could behave with their employee. If McLaren and their sponsors wanted, they could have sacked him and the FIA nor even Bernie could have done nothing about it. UK Employment Law > some stupid FIA ruling and their fake "court of law".