Order of authors in a paper I have worked on few research labs and performed research as a master's student with PhD students and faced similar issues. 

It has been papers that I have contributed most of the work for that paper, including producing results, and writing. Yet the PhD student wants to put my name second due to seniority. 

When I first got involved I understood that the order of the paper authors is important in showing the amount of contribution. It feels extremely demotivating to know someone else is getting credit for the very hard work I done and makes me want to stop being involved in research. 

Questions:


What is the best practice to decide the author order for CS/AI/ML related conferences?
When everyone has contributed equally and ordering by name, is it the first name or last name you order by?
When the order of the authors doesn't match the level of contribution, what is a good way to bring it up and to who?
Rule #1: Always try to pin these types of things down before performing the work.
Rule #2: Understand that academia (graduate school) is run on an implicit pecking order:

Departmental pressures and requirements &gt; Advisor &gt; PhD Student &gt; Master's Student

This makes complying to Rule #1 very difficult in practice. Every early career researcher runs into the issue you have described in some form. (Not that this makes such practices right). People higher than you in the pecking order will use administrative leverage to get what they want.

Advisor: &quot;I am the first author on this paper.&quot; (And if you disagree, you'll have to find a new advisor and lose a year's worth of work).
Department: &quot;Give Professor Smith what she wants or we will not sign off on your degree.&quot;
PhD Student: &quot;Collect this data for me. (And then try to convince someone that you actually did the work and not me).&quot;

The order of attribution should be based on the order of contribution in most CS-based fields. Equal contributions are noted by alphabetical order of the authors' surnames, sometimes with a footnote indicating equal contribution.
I would bring the issue up first with the PhD student directly. I would then speak with his/her advisor. The advisor is essentially the court of last resort. An advisor also will be able to help with the navigation of what actually constitutes authorship for each party. At the very least, your own advisor should (hopefully) be able to help you obtain credit for your work in the form of a chapter in your thesis or something.
Going forward, I would be firm in establishing what the authorship expectations will be on any work you do. You will at times be forced to balance fairness in authorship and expediency in obtaining your degree.
I will note that I take the pecking order things into account when I interview applicants for a job. I care much less about author order and much more about what the person actually did for the paper. Thus, if the OP applied to a job with me, I would likely give his research the same weight whether he was first or second author. I fully understand that superiors sometimes take advantage of their underlings.