In his announcement (at 20:02) of the special military operations, Vladimir Putin invoked article 51 of the UN charter, justifying these operations as self-defence. However, since Russia was never attacked by Ukrainian shelling, which was being carried out in Donbas and the proclamation of independence of Donetsk and Lugansk People's Republic didn't make them part of Russia, how did Russia invoke article 51? Can other states invoke article 51 for states they are allied with?
Russia's justification of its invocation of article 51 is based on its recognition of the DPR & LPR as independent states, combined with articles 3 and 4 of the treaties "of friendship and cooperation and mutual assistance" (Luhansk, Donetsk) ratified by the State Duma on February 22nd, which enshrines a commitment to mutual defence. The UN Charter explicitly recognises the right of collective self-defence in Article 51:
Nothing in the present Charter shall impair the inherent right of individual or collective self-defence if an armed attack occurs against a Member of the United Nations, until the Security Council has taken measures necessary to maintain international peace and security. Measures taken by Members in the exercise of this right of self-defence shall be immediately reported to the Security Council and shall not in any way affect the authority and responsibility of the Security Council under the present Charter to take at any time such action as it deems necessary in order to maintain or restore international peace and security.
Article 3 and 4 of the above-mentioned treaties set out the basis for the mutual defence agreement, and article 4 even references the right to collective self-defence recognised by the UN Charter.
The Contracting Parties will closely cooperate with each other in the defense of sovereignty, territorial integrity and security of the Russian Federation and the [Donetsk/Luhansk] People's Republic. They will consult without delay each time when, in the opinion of one of the Contracting Parties, there is a threat of attack against it, in order to ensure joint defence, maintenance of peace and mutual security. During these consultations, the need, types and amounts of assistance that one Contracting Party will provide to the other Contracting Party in order to help eliminate the threat that has arisen will be determined.
The Contracting Parties shall jointly accept all measures available to them to eliminate a threat to the peace, a breach of the peace, as well as to counteract acts of aggression against them by any state or group of states and to provide each other with the necessary assistance, including military, in the exercise of the right to individual or collective self-defence in accordance with Article 51 of the UN Charter.
Of course, neither the DPR or LPR are members of the United Nations, so by my reading of the charter, article 51 would not apply in this case.
To rephrase this for us not-so-bright guys, Russia is using the old, old trick "we're not invading -- we were invited to defend the true government from these usurpers".
The US tried this back in the Bay of Pigs. The invasion force wasn't expected to beat Cuba's army. They were supposed to do just well enough to seem like a rebel movement, then call in help from the US. The US invasion of Iraq was the same. It was partly covered by the Iraqi National Congress. These were Iraqis in exile, and they were a "Congress", so it was like "see, the real government is asking us".
That stuff is all pretty weak, but it's important to have something to tell your own citizens. It helps other countries who want an excuse to stay out of it. It's good for the history books if you win, to say you were a liberator.
On to Russia. You may recall that years ago, Putin made a big deal about how the more Russian-speaking part of Ukraine is being oppressed. He sent in disguised Russian troops which took over a small area and eventually called it a new country. 150 years ago the US recognized part of Mexico as a new country named "Texas" and got away with it; so the trick works.
This new claim makes no sense — Russia is defending a small part of Ukraine that they partly conquered from possibly being taken back? But it makes as much sense as those things always do. It's also the totally obvious thing to say. The only strange part is how they didn't seem to be able to find some Ukrainian mayor or something to say it for them: "as a loyal representative of New Eastern Ukraine, we welcome Russia's aid".
how did Russia invoke article 51
However the validity of this approach has also been rejected by for example the secretary general of the UN expressed that "such a unilateral measure conflicts directly with the principles of the Charter of the United Nations – and is inconsistent with the so-called Friendly Relations Declaration of the General Assembly which the International Court of Justice has repeatedly cited as representing international law."
Also the armed attack in article 51 "...inherent right of individual or collective self-defence if an armed attack occurs against a Member of the United Nations" has been highly contested. In the end, it was the other way around. Russia started an armed attack on Ukraine instead.
So additionally to DPR and LPR not being members of the UN as pointed out by CDJB's answer the required armed attack on DPR and LPR is problematic.
Finally article 51 speaks of keeping the peace "...measures necessary to maintain international peace and security....". Russia failed spectacularly at that. Two days after that laws were adopted they invaded the remaining parts of Ukraine which is the opposite of maintaining international peace and security.
All in all it looks like a distortion of the language of international law, where self-defense is taken as justification for a subsequent invasion of a foreign country. The meaning of peace keeping is more or less completely lost in that context.
Without any grounds
The chapter 51 allows self defense in case of the armed invasion against a member of UN. There was no armed invasion as done by Ukraine into the territory of Russian Federation. V.Putin talks about the DPR and LPR. These are not members of UN.
There is no ground to apply this chapter.