There is no formula. Most of it is about luck. But I think two things can help you.
First, learn languages. The more the better. I don't want to blow my cover, but I been incredibly lucky in my undergraduate and masters, and got to do them at two very good universities filled with brilliant students. In both cases, the stand out kids were those who could do things not only in French, German, Spanish, Italian, etc., but also Russian, Chinese, Japanese, Arabic, Polish, Norwegian, etc. The broader the range, the better- irrespective of what period/nation one is working on. We do a very poor job of teaching foreign language skills to kids in the US, so you will stand out if you have a wide range under your belt. If I could redo undergrad, I would devote more of my time to studying languages than to taking history courses. Wide-ranging linguistic ability can enable you to think about a whole set of projects that are otherwise unfeasible. The best thing you can do for your prospects over the summer is study languages.
Second, come up with an innovative writing sample. I don't think it necessarily has to be about history. Rather, choose work that displays your ability to think creatively about a problem (especially if it is one people thing has been "done to death"), and write about it in a clear, precise, and concise manner. I think it can really help if you can demonstrate what's innovative or new about your approach or insight. Sorry if this seems abstract, but am trying to avoid revealing myself.
Hoping to get lucky again for the Ph.D! I am pleased to have some very good options so far, for which I am exceptionally thankful, but am increasingly pessimistic that I will get into the exact places I want. Such a crap-shoot.