Will a D in a major elective keep me from getting into a PhD program? I am in my last semester of undergrad (majoring in mathematics) and am currently working on applying to PhD programs (in mathematics). During the fall of 2016 due to a health issue that lasted for the majority of the semester and the general difficulty of the course I received a D in Topology. Topology is not a required course at my university, but is an elective in my major. I was going to immediately retake the course, but my school has not offered the course since I last took it until this semester (fall of 2018). So, I could retake it this semester (and am currently planning on it), but my applications will need to be submitted before the new grade can be recorded, so I will have to submit my application with the D on my transcript. 

I have done everything in my power to try to remedy this flaw on my transcript. I have a ~3.7 (3.68 exact) overall GPA and subject GPA (although if I had the chance to retake the course my mathematics GPA would be a ~3.8), as well as having received permissions from the department to take several graduate courses as an undergraduate in which my GPA is a 4.0 and taken several courses that cover and use topological concepts and received A's in them. I strongly believe that given the chance to retake the course I would have no problems receiving an A (or using topological concepts in research/other classes) since I have already mastered topological concepts in a variety of other courses as well as my prior knowledge from my first time taking the course.

I also have prior research experience from my freshman year (in a different engineering area since my freshman year I was a computer science major) and will also be starting mathematics research during this coming semester, so I will most likely not publish anything but will have research experience. My GRE scores are average to slightly above average according to GRE data that I have looked at and will be retaking the GRE to improve my scores even further next week and my letters of recommendation will be strong as I have worked closely for years with all of my recommenders.

Will this bad grade effect my chances of getting into a PhD program? Should I apply to a masters program first? Should I retake topology this semester even though admissions committees will not see the new grade until after I have received a decision? Any advice would be appreciated!

(Note: I have read a handful of other posts on here about one bad grade, but none seemed to quite match the dilemma of a bad grade in an upper level elective in one's field of study. Thanks!)
I don't believe any single thing like a particular grade is disqualifying, but it will raise eyebrows in anyone considering you for acceptance. But things happen. If the grade is truly an outlier and you have a good explanation most people will understand. Not all, of course. 

Stress the positives in your background, as you have in this question. Don't ignore the negatives. 

For future reference, however, if it is possible to drop a course late, do so when you are having trouble. Often a Dean's signature may be required for a late-drop, but he/she will be in a position to evaluate your need to do so. I once took too many credits, got into difficulty, and dropped a course to get back to a more reasonable workload. In my case it wasn't difficult, but in the case of illness, especially, most folks at your institution will make an exception to a rule. That doesn't help you now, of course. 

Making an attempt at re-learning, even without a grade, is a positive thing. You can mention that.

Different Universities feel differently about accepting doctoral students straight from undergraduate. It likely also depends on the field, but it is possible in Math, of course. Sometimes a Master's degree is just awarded along the way as a sort of marker. So, while it depends on the University, I don't think that "settling" now for a Master's is a good choice.