The Iroquois used the earliest known method of companion planting when they grew what is called the Three Sisters, the practice of planting corn, beans and squash together. They knew that these plants were compatible with each other and each one provided nutrition that the others needed.
Corn needs plenty of nitrogen to thrive and beans fix nitrogen in the soil. The corn then provided a “trellis” for the beans to climb on. The bean stalks provided extra strength for the corn. Planted at the base of the corn and beans, the squash provided mulch on the soil. It’s widespread leaves kept the soil cool, held in moisture, repelled weeds, and made it harder for animals like raccoons to get to the corn.
You can have this same harmony among plants in your garden even if you don’t grow corn, beans and squash together. For example, tomatoes get along well with carrots and like marigolds and nasturtiums at their feet to confuse pests. Other plants also like these two flowers as pest deterrents.
At the same time, vegetables also have neighbors they dislike very much. Cucumbers should never be planted beside any of the nightshade family of plants (tomatoes, potatoes, bell & chili peppers, and eggplant) because nightshades contain alkaloids that are highly toxic to some other plants. Nightshades can attract pests that seek out cucumber vines.
Other bad companions include onions and garlic. Don’t plant asparagus, beans, or cabbage near them. Some herbs like fennel aren’t compatible with any other plants, so plant it off by itself. Butterflies like to lay eggs in fennel foliage so give them their own space.
For a comprehensive list of companion plantings, check out The Vegetable Gardener’s Bible by Edward C. Smith. This is a great all around gardener’s guide to growing all kinds of vegetables.