What does Kant mean when he says rational beings exist as an end in themselves, and never merely as a means to an end?
Humanity as an end in itself is one of the four formulations of the Categorical Imperative, and is found at 4.429 in the Groundwork for the Metaphysics of Morals.
There may be similar formulations in The Critique of Practical Reason or The Metaphysics of Morals but I'm not entirely sure.
"End" for Kant is anything that provides reason or motive for acting, which includes values.
The OP caught on to something important: humanity for Kant is a rational nature and includes the ability to set ends.
For Kant this we can only rationally value our own existence if we value the existence of other rational beings. In short: we have a moral duty to seek ends that respect the rational nature of other human beings, and the only two types of ends that satisfy this requirement are 1) our own perfection and 2) the happiness of other human beings.
It is perfectly permissible in Kant's view to treat other rational beings as means. I do so whenever I call a taxi and the driver agrees to take me to my destination. The taxi driver has freely consented for me to use her/ him as a means in this way.
But of course, Kant prohibits the use of another rational being as merely a means. The following extract might help to explicate the idea:
For example, if I use your essay without your knowledge then I have not treated you as a rational agent. I would have done had I asked you for your essay and you had freely chosen to let me have it. But given that I did not ask you, I was in a sense making choices on your behalf and thus did not treat you as a rational agent. So according to Kant I should always treat you as an end not a [mere] means. I should always treat you as a free rational agent.
Kant’s theory then has a way of respecting the dignity of people. We should treat people with respect and with dignity purely on the basis that they are rational agents, and not because of their race, gender, education, upbringing etc. From this you can also see that Kant’s theory allows us to speak about “rights”. If someone has a right then they have this right irrespective of gender, education, upbringing etc. For example, Jill has a right to free speech because she is a person, consequently that right will not disappear if she changes her location, personal circumstances, relationship status, political viewpoint etc. After all she does not stop being a person. (Mark Dimmock and Andrew Fisher, Ethics for A-Level, Open Book Publishers. (2017): 38.)