I think we've talked before, but I do want to make it clear. I know of a home on the market right now in Montclair, with an lovely illegal unit. The City has required it to be removed before close of escrow. I believe in this case the neighbors complained, but this again, is always possible.
Worst case scenario could mean disassembling the unit...though I've never heard of that happening. And they probably could prohibit you from renting that space. There may already be laws or regulations in your area about number of residents (or unrelated residents) in a property. A nearby county (Prince William County in Virginia) a year or two ago passed stricter laws on the number of unrelated residents. A lot of homes were being used almost as boarding houses, often by "undocumented workers." They then enforced the new law, to the delight of some and to the dismay of others.
More likely, if the authorities had a problem, they could require that the unit be brought up to code. That's sometimes done. And that can be a relatively easy process if the apartment was constructed to code, or a nearly impossible process if it wasn't.
As for getting a permit, that varies by jurisdiction. Some are very willing to grant permits--especially if there's a shortage of affordable housing in the area. Some are very reluctant to do so.
Best bet would be to speak to a local real estate attorney. He/she would know the applicable laws and regulations, as well as have a good sense of the politics involved if you needed or wanted to get a permit or bring the property up to code.
Hope that helps.
There are varying degrees of this. I am selling a "legal" unit in a home meaning the City knows about it. It was permitted and received a "conditional use permit" for the caretaker of the owner.
The worst case is that the City doesn't know, it isn't built to code and you are required to remove it. In that case you are buying a pure expense rather than an income unit. Getting a permit after the fact is difficult and would require that everything meets code. You will have to open up walls etc...
Proceed with caution.