It COMPLETELY depends on the bacteria or virus.
TB, for example, can live for several days. HIV can only live for moments. Some bacteria and viruses require a live host, others can encapsulate and live for thousands of years with no host. We've gotten encapsulated microbes out of tombs and other archeological sites that are crazy fascinating to study (they're also the culprit behind most "cursed tombs"... once they hit a live host, they uncapsulate and start replicating like crazy). ALSO what conditions a microbe can survive in varies a great deal. Some can take heat, others can't. Some can take cold, others can't. Some need moisture, others don't. The list goes on.
The 'common cold' is a series of VERY hardy viruses, it's part of why they're pandemic... but how long they can live outside a host varies per each strain. Some for only a few hours, some for several days.