It is part of your job to represent your employer and convey a certain image to customers.
For a uniformed service job it's a no-brainer. You are expected to wear the company-provided clothing, which typically serves additional purposes: hygiene, protection, branding (logos) or simply identification (if you walk into a hospital or a McDonalds, you want to be able to tell who are part of the staff).
For an office job you have a lot more individual freedom, but it still really comes down to "does my way of dressing help the business?". For an extreme example, showing up in an SS uniform would be inappropriate anywhere. However, it is quite rare to be fired on the spot for minor deviations - employees are more valuable than that (it costs more to replace them than correct them). It will usually escalate through several meetings, with increasing degrees of warning, before leading to termination - though at that point, if the employee has been explicitly told what is acceptable and still won't comply, it is no longer about "dress code violations" but a more severe problem of insubordination. Simply put, your freedom of expression is secondary to the interests of the company.
There are several reasons employers might not want to put their informal dress code down in writing. Foremost, managers want to be able to decide at their own discretion what will fly and what won't. Working out a specific code also takes a lot of careful work, as a sloppy effort can lead to complaints about sexism or cultural insensitivity. Another issue is that many people have a "loophole" mentality and a written code won't necessarily help if there are employees already set on not following the code. For example it may dictate wearing a suit, and then people will mock the rules by show up in garish colors or with ridiculous ties.
An interesting example of subversion occured in Sweden a couple years ago. During a hot summer, bus drivers were complaining about the heat and having to wear all-black clothes and long pants, with no option for shorts. Male drivers protested by wearing skirts, as these were sanctioned by the dress code, even if they were only intended to be worn by women.