There's three broad categories of unit "costs."
Unit upkeep. Add up the total number of units you have (they all count as 1 each), multiply that total by a difficulty level scaling (and the AIs get their own reduced costs, which sometimes helps them sustain armies that would bankrupt a human player), subtract off the number of "free" units you get (which primarily depends on your empire's population), and that's your unit upkeep.
Unit supply. Same general idea as unit upkeep, but slightly different formulas and it's only counting units outside your cultural borders.
Military unit upkeep. If you're in the Pacifism civic, you pay 1 gold per "military unit." Workers, settlers, privateers, and great people don't count.
There's also some tags and logic in the game files that could potentially allow more variety in unit upkeep costs; it's just that nothing in the game-as-shipped actually makes use of them. For example, iExtraCost in the unit infos .xml file could potentially add additional cost for specific types of unit, except every single unit in the game by default has an iExtraCost of 0.