cheat codes were development / QA tools to make testing the game easier. They got left in because they were behind hidden, strange button sequences etc, removing the code risked breaking something that would be harder to test without the codes, and they can be fun.
That's maybe how they started, but between then and now was a time when developers would very specifically add in cheat codes that had nothing to do with development or debugging, and were often just extra things added in to make the game more fun to play. (See 'paintball mode' in Goldeneye N64 for a prime example of that.) But those kinds of cheat codes seem to have fallen out of fashion.