Should I give up on trying to get into a PhD program?
Based on what you've said, there is little to no evidence that you are "just not good enough" for a PhD in linguistics, so it would be a mistake to not apply again for that reason.  Rather, you should try to become more "genre savvy", 
which in this case means learning more about the application process and what PhD programs in your field are looking for.

Here are some mistakes -- or, at least, opportunities for future improvement -- 
that I can see based on what you've said.

1) The writing sample: To be honest, I'm not sure exactly what this is.  In my field (mathematics), we have a "personal statement", which may or may not be similar to a "writing sample": in fact, it mostly functions as a writing sample, as what is expressed is usually not as important as the clarity of the writing and the reasonableness of the sentiments expressed.  But I don't know what it means to contribute a writing sample "in philosophy" versus "in linguistics", and I really don't know what it would mean to spend a year improving the writing sample.  I hope that academics in these fields can comment and clarify this point. My best guess at the moment is that you are actually supposed to submit a piece of academic writing in the chosen field.  If so -- applying to a linguistics PhD program and submitting a writing sample from philosophy is pretty strange: given that you took five graduate courses in linguistics, you certainly didn't need to do that.  Why did you do that? 

I would not take as a given that your writing sample was the main problem -- rather, you should find out; more on that later -- but I would definitely take steps to improve your writing sample the next time around.  You wrote::

"And my professor was supposed to look at my writing sample in linguistics, he didn't end up giving me comments. I got comments from a PhD student who knew his stuff really well so that helped, and he said my sample was good."

You are not getting enough help here.  A professor who looks over something like this and has no comments is not helping you at all: it doesn't mean your writing is good or bad or anything: it just means you need to find a different mentor.  Getting comments from a PhD student is much more helpful than nothing, but a PhD student will have absolutely zero direct experience with PhD admissions, so he cannot directly help you.  You need to find at least one faculty member in linguistics who is willing to give you significant feedback.

2) Programs applied to: In my field of mathematics, most students apply to about 6-12 PhD programs.  You could get away with applying to the lower end of this range if you choose carefully: almost all students should apply to at least three different tiers of programs.  The lowest tier should be such that your faculty mentors say that they expect you to get in there.  In my field, anyone who would get waitlisted at a top program could certainly get admitted to a lesser (still wholly reputable) program.

Then next year you apply again to only 9 programs including the world number two program in your field.  Having had to wait an entire year because you "almost" got into a PhD program, you should have applied to many more programs the second time around and also included a lower tier of programs than you did the first time.  While I won't say it was a mistake to apply to the world number two, it is also not so clear how close you came to being admitted.  The waitlist is still an ordered list.  Unless they said you were at the top of the waitlist, it does not necessarily mean that you were very close to being admitted, especially since they told you that they didn't go to the waitlist at all.  (Also, for PhD programs you should be making a clear distinction between admission-with-funding and mere admission.)

I would suggest that you apply again to PhD programs, to more of them, and with a larger range.  Get a faculty mentor in your current master's program to help you with this.  

3) Guidance and feedback: You have not gotten good advice.  It's hard to get good advice, but it can be almost impossible if you don't stick your neck out and try to get it.  I strongly recommend that you write to each of the programs that waitlisted you, carefully (though relatively succinctly) describe your situation, and tell them how helpful it would be to get feedback about improving your application.  What is the one thing you should work most on improving?  In particular, you have a golden opportunity to do this at your own undergraduate institution.  For that, I would suggest an in person visit.  When you talk to your old professors in person, they are really going to have to open up and tell you what happened.  You don't want to be bitter or confrontational about it: rather you should make clear what you are doing, which is getting the feedback you need to improve your application.

In summary, based on what you report about your profile, I myself am surprised that you did not get into a PhD program, given that you applied to 17 of them over two years.  I think you're right that people with worse records than yours are getting admitted.  This means that something in your application is not as it should be.  It is your life, after all, so it is worth devoting some energy to the sleuthing here: really suss out the problem before you apply again.

Good luck.