What are all of the necessary, essential, and optional preventative medications dogs need (e.g. heartworm pills)? By medication, I do not mean vaccinations that dogs need, but rather medications like pills.
I know this answer might be unpopular, but the one person who knows best what your dog needs is a vet. As we established in the comments, this is an international community, so the environments our dogs live in are so vastly different that there cannot be one answer that fits all.
Your local vet knows best what kind of ailments they treat regularily in the area and therefore which preventive medications are worth it. In many cases it's enough to call them and they give you a recommendation for free. They can also give you tips about first aid for dogs: what symptoms to watch out for (in case of poisonings, infections or pain) and what items to have in your first aid kit.
Please keep in mind that your vet should see your dog at least once a year anyways for a routine checkup and to refresh the vaccinations.
I live in a very rural way, my dog is best characterized as a farmyard dog who has access into the house. Even though the dog roams our grounds, we've had fox puppies in a wood pile and you may see some roe deer browsing while the dog snores in his hut maybe 25 m away. Which means that my dog has an unusually high exposure to wildlife parasites and diseases.
Under these circumstances, I regularly deworm the dog and he gets a preventative spot-on repellent treatment against flea, ticks and mosquitoes. More frequently when there are lots of ticks around since the repellent property against ticks doesn't last as long and we do have lots of ticks, Lyme disease (where I ask myself why there isn't a spot-on also for humans, the antibiotic regime isn't that much fun and it also doesn't always work) and also tick-borne encephalitis (against which we humans vaccinated since back when that was available only based on actually high exposure to ticks), so they are the primary concern. He's also picked up flea, probably by saying hi to either the fox or a hedgehog (or a racoon or ...), but they left him within hours, so the repellent seems to work nicely.
I'd say both treatments are optional in the sense that I wouldn't think any worse of a dog owner who deworms only when actually needed, and does not go for the repellent.
For a "downtown" dog who spends its life indoors and is walked only on streets and maybe in the park and at most occasionally has exposure to rural settings during a strictly leashed weekend hike, I'd probably deworm only before vaccination. I'd then consider preventative flea treatment very much a matter of personal preference. On the one hand, it would be unlikely to ever pick up flea, but on the other hand they'd be guaranteed to be released in the flat.
- Heartworms are so far no concern over here, though they are known e.g. in the Mediterranean region.
- Don't forget that whenever your dog has flea, you need both anti-flea treatment plus deworming since cat flea (the most common flea over here) can transmit the flea tapeworm.