There a various methods that all fall under the heading companion planting:
There are trap crops, sometimes a neighboring crop will be planted as it is more attractive to pests and draws them away from the main crop or plants. A good example is planting four o'clocks for Japanese beetles, or radishes to attract bean beetles and squash bugs away from your main crops. There are rhizobium bacteria with act in symbiosis with peas, beans and other legumes to act to fix atmospheric nitrogen. There by providing additional nitrogen to crops planted around and in that spot when the legumes are finished.
There are certain plants that exude chemicals from their roots systems and flowers. They act as a repellent to may garden pests. Using African marigold releases thiopene-a nematode repellent-good for using in many garden areas. Sometimes biochemical effects can allopathic in nature and become a problem in a home garden. An example being the juglone produced by black walnut trees. Care must be take in the choice of what you might be planting under or around these trees as the juglones they produce can stunt or greatly weaken other plants.
Also to be considered is spatial inter planting. Tall sun loving crops can be paired with lower growing shade loving plants to greatly increase production of the area. This arrangement can also provide a certain measure of beneficial pest control. Inter planting of prickly vining plants in corn is said to help deter raids by local raccoons. Another variation of this method is a nurse crop. Taller crops have a tendency to shade out weed seed germination and aid in allowing a better start to lower and longer growing crops.
Recently using companion planting as a way to draw in and nourish insect beneficials has received more attention. Encouraging these helpful insects, especially the predatory and parasitic species greatly aids in keeping the pest population in check.
And lastly, a more general mixing of various crops and varieties provides a degree of security to the grower. If pests or adverse conditions reduce or destroy a single crop or cultivar, others remain to produce some level of yield.
There are numerous websites and books available to check to get specifics about which plants make the best companions for other plants.
I highly recommend Great Garden Companions by Sara Jean Cunniham as one example.
Credits for the photos from the DG Plant files: Gabreille, Jody and dashy169. Thanks for sharing your photos, ladies!