Plants, being anchored in position, have a special problem in this regard. Many employ the services of animals (e.g., insects, birds, bats) to transfer pollen from plant to plant. But if the flowers have both sex organs:

Dioecy. The imperfect flowers are present on separate plants. Dioecy is the equivalent of the separate sexes of most animals. But it is rather rare.
Some examples:

poplars

hollies

Monoecy. The imperfect flowers are present on the same plant. But if they mature at different times, self-fertilization is avoided. Corn (maize) is a common example.

But the vast majority of angiosperms have perfect flowers; that is containing both male and female sex organs. So how do they avoid self-fertilization?

Heteromorphic flowers.

The flowers are perfect but come in two structural types; for example

long stamens with a short style and

short stamens with a long style.

A single plant has one type or the other.

If the pollinator has a short tongue, pollination is favored from the first type to the second — but not the reverse.

Heteromorphic flowers are not common, and even in the angiosperm families that favor them (e.g., primroses, flax), the same biochemical mechanisms of self-incompatibility that we will find (below) in homomorphic flowers are usually present as well.

Homomorphic flowers. All flowers have exactly the same structure. Avoidance of self-fertilization depends on genetic/biochemical mechanisms. There are two quite different types of self-incompatibility.

The control lies in the "S-locus", which is actually a cluster of three tightly-linked loci:

SLG (S-Locus Glycoprotein) which encodes part of a receptor present in the cell wall of the stigma;

SRK (S-Receptor Kinase), which encodes the other part of the receptor. Kinases attach phosphate groups to other proteins. SRK is transmembrane protein embedded in the plasma membrane of the stigma cell.

SCR (S-locus Cysteine-Rich protein), which encodes a soluble ligand for the same receptor which is secreted by the pollen.

Because the plants cannot fertilize themselves, they tend to be heterozygous; that is, carry a pair of different S loci (here designated S1 and S2).

However, dozens of different S alleles may be present in the population of the species; that is; the S-locus in the species is extremely polymorphic (analogous to the major histocompatibility locus of vertebrates — Link).

The difference between the alleles is concentrated in certain "hypervariable regions" of the receptor (analogous to the hypervariable regions that provide the great binding diversity of antibodies — Link).

The rules:

Pollen will not germinate on the stigma (diploid) of a flower that contains either of the two alleles in the sporophyte parent that produced the pollen.

This holds true even though each pollen grain — being haploid — contains only one of the alleles.

In the example shown here, the S2 pollen, which was produced by a S1S2 parent, cannot germinate on an S1S3 stigma.

The explanation:

The S1S2 pollen-producing sporophyte synthesizes both SCR1 and SCR2 for incorporation in (and later release from) both S1 and S2 pollen grains.

If either SCR molecule can bind to either receptor on the pistil, the kinase triggers a series of events that lead to failure of the stigma to support germination of the pollen grain. Among these events is the ubiquination of proteins targeting them for destruction in proteasomes.

If this path is not triggered (e.g., pollen from an S1S2 parent on an S3S4 stigma, the pollen germinates successfully.

changes in flower structure to reduce the chance that pollinators will transfer pollen from another plant to its stigma.

Unlike its wild relatives, the stigma of the domestic tomato does not protrude beyond the anthers. Of the several genes involved in this change, the most important one is Style2.1. The mutation in Style2.1 responsible for the change in phenotype in our cultivated tomatoes is found in the promoter region — the protein-encoding portion of the gene is exactly the same as in wild tomatoes.

Here, again [Link], is evidence that much of the diversity of life arises not from mutations in the protein-coding portion of the genes that we share but mutations in their regulatory regions (promoters and enhancers).

Some animals are both hermaphroditic (have both male and female sex organs) and sessile (anchored in one place). So, like the plants discussed above, they also face the problem of avoiding self-fertilization. The sea squirt, Ciona intestinalis, uses a SI system that functions much like SI in plants although the recognition molecules are entirely different.