You are confusing proximate and ultimate causes.
What causes an organism to keep eating, most are not smart enough to know they will die if they don't eat. The ultimate reason may be to keep from starving but that is not the proximate reason.
The proximate reason they eat is because their brains are built to want to eat when their senses detect food. to get hungry and feel pleasure when that hunger is sated.
eating, drinking, scratching, pain, disgust avoidance, vomiting, reproduction, they all come with basic urges, proximate motivations. the brain is built to want to do these things under the proper conditions. Why do songbirds sing, because they want to, they have a desire to and acting on that desire in the correct circumstances gives them pleasure or not acting on it causes discomfort.
Animals have sex because they want to, that is the proximate cause. The ultimate reason they want to is brains with that desire are the brains most likely to pass on their genes to the next generation. Thus genes that make brains that get the animal to reproduce are the genes most likely to make it into the next generation.
To look it another way you are the product of an unbroken line of genes that built brains that reproduced, because genomes that did not don't leave descendants.