How do viruses or bacteria survive outside the body long enough to spread?
It depends entirely on the environment the virus or bacterium is in and also the size of it. 

Virus

With regards to viruses, the most important thing is typically whether or not the virus has an outer membrane or envelope.

Non-enveloped viruses such as rhinoviruses, noroviruses and adenoviruses can in fact survive for long periods outside the host (up to several days) whereas viruses with membranes such as HIV, influenza and the herpes viruses which do well in the host survive for shorter time periods.

Experiments with rhinoviruses and influenza viruses have shown potential survival times ranging from a few minutes to 48 hours or more.  They remain active longer on stainless steel, plastic and similar hard surfaces than on fabric and other soft surfaces. 

This is because many enveloped viruses rely on the proteins on the surface of the membrane to attach to the host cell, this envelope is generally sensitive to degradation to sunlight and normal cleaning procedures.

Outbreaks associated with the non-enveloped viruses can continue for many months due to their relative stability in the environment, think of winter vomiting outbreaks, hand foot and mouth in animals as good examples of this phenomenon. 

Bacteria

Because bacteria can undergo metabolism, synthesise proteins etc. for themselves, their life span is usually longer. If we split them into gram positive and gram negative its easier to discuss. Gram positive bacteria have a protein coat that prevents drying allowing them to survive longer. An example is the bacteria Staphylococcus aureus, which is responsible for toxic shock syndrome, food poisoning, and wound infections. With its protein coat it can withstand temperature and humidity extremes. The spore may survive for weeks on dry clothing using sloughed skin cells for food. The Bacillus anthracis, the anthrax bacteria, can also form spores and survive tens to hundreds of years. This is in comparison to gram negative which need humidity. An infamous example is Escherichia coli which survives typically only a few hours at best.