(Math PhD): I think two things helped me the most:
Three strong recommenders (all in my target subfields) including senior thesis advisor.
13 grad courses (12 math, 1 CS) + 1 grad-level reading course in 3 years, both in and around my desired subfields and in breadth.
Other notes:
I wrote a relatively standard and boring SOP, but it clearly stated what I did before, what I want to do, who I want to do it with, and why I want to do it at [institution]
The resource/opportunity/having smarter peers advantage of the elite schools is significant, but nowhere near insurmountable (undergrad outside top 50 -> admitted to top 5)
Advice:
take as many grad courses as humanly possible
Do research if possible, but don't sweat not having publications (none of the 4 people admitted to a top 5 department from my school have had one)
don't bomb the subject GRE
build relationships with faculty early
I don't believe emailing faculty is necessary