Some residence titles are tied to a specific employer, sector or region and some are not but leaving a job is never a crime and is not punishable as such. The only problem is that if you do that before you have another job and authorisation lined up or some other legal basis to stay in Germany (e.g. you married a German resident), your current residence title could become invalid and you would therefore have to leave the country or become liable for deportation.
Because of this, you have to report the fact that you lost your job to the foreigners registration office (Ausländerbehörde) immediately and they will decide how long you are allowed to stay. I believe they can grant you a grace period to bridge the time between jobs or even a six-month permit to look for another one but you are not entitled to that.
If you want to switch jobs during the first two years of your stay in Germany, you need to have your residence title amended (see e.g. berlin.de). If it's a better job (in the sense that it's a job in the same sector, with the same or better salary), I think it should be fine but it's not automatic (not really easy but doable). Your current employer does not have to consent to this, it's entirely up to the authorities.
After the first two years, you can switch jobs without prior authorisation, as long as the new job still meets the same conditions (e.g. still a highly-qualified occupation, etc.). You still face restrictions (for example, you could not take a random low-paying job that you find more fulfilling) but you are not tied to an employer in any way.
Finally, after five years and if you learn the German language, you would become eligible for a kind of permanent residence permit called a Niederlassungserlaubnis. Once you have that, you can stay in Germany with no time limit and take any job you like (not only jobs approved by the federal employment agency based on the needs of the German economy).