I've seen this happen.
My supervisor wrote some software at my old workplace that seriously streamlined the things we did because we were doing so much bullshit on pen and paper it was ridiculous, and soon this piece of software became critical not just for the actual work but for a timeclock and a dozen other functions. He was not in a programming position; he used his personal experience to create the software far beyond his pay grade and position. The off-site executives had no idea about it for a long time; when the suits found out about the software they took it over. When he resisted the process, the political machine kicked in and he was fired/forced to resign. A nightmareish third-party development house took over the app in theory - and proceeded to not do a damn thing with it.
Worse for us who were left - code maintenance went into a limbo that left us using a piece of software unable to evolve with our workflow needs. It degraded over time until it was barely useable at all; none of the many bugs and features that had been on his "to-do" list were left unattended. But by that point we were tied to this software. We'd have had to go back in time and redesign our entire previous paper-based process from scratch, losing really tremendous time and productivity in an already tough contracted environment. It was a total nightmare. I eventually left the company and I have no idea what happened after that, except that I know my project has long since been essentially closed anyways.
So: work it out ahead of time with your bosses that you might be interested and do not, do NOT, DO NOT let them know that you've written a SINGLE line of code while under their employ. There's a good chance they'll try to simply take it from you. Even if it's not in your contract, they may still try to make a claim. Even if you think they're cool, even if your boss is your bro. Get any deals, compensation, or stipulations worked out on paper ahead of time if you don't want to simply hand them your work for a fraction of what it's worth. And consider the life cycle of your software, and how it will impact the company and people who use it as a whole.