Soviet political officers (Commissars) had their origins in the February Revolution. They were an invention of Alexander Kerensky (Minister of Defence for the Provisional Government), and there were a number of expedients at play. During this Liberal stage of the Russian Revolution, the fledgling democracy was still reliant on the Imperial officer corps.
The motivation of these men was varied - some were seeking the prestige of a higher position than they previously had held, some needed the money, some hoped to avoid the Red Terror, and some eventually came around to the Bolshevik position1. Their position in the Red Army was a point of contention, because they were a remnant of the old military regime - the very hierarchy that many soldiers had thought the revolution was about destroying. Little did the soldiers realize that the main threat to their freedoms came not from these officers, but from the Bolsheviks.
The Commissars were not responsible to the normal military chain of command. They existed to make sure the ex-Tsarist officers discharged their duty properly, and as an extra motivation they themselves were the first to be shot if a unit went wayward. All orders had to be signed by both the officer and the Commissar, but opponents of the inclusion of ex-Tsarist officers in the Red Army were quick to complain that Commissars had no real power.
This was a convienient way for them to find a scapegoat, and it led to Leon Trotsky increasing the powers and influences of the Commissars after the October Revolution. Still, some were viewed as burzhuis (class enemies) by the soldiers, along with the ex-Tsarist officers. This was especially liable to happen to ones who dressed well or acted as autocrats - such was against the spirit of the "proletarian revolution" that the soldiers thought they had signed up to.
One of the main tasks of these political officers was conscription of the local peasants. This had been opposed mightily by some of the Bolshevik Party leadership because it went against their principles - it was supposed to be a Party of the working class, not of the peasants (who were often viewed as reactionary). However, just as with the recruitment of ex-Tsarist officers, military expediency eventually took over and it became necessary to conscript peasants in droves.
The peasants were often hostile to this (after World War I they thought they had been at war long enough) and resisted violently the Commissars attempts at conscription - and as the Soviet Republic was declared a "single military camp" subject to martial law in the Summer of 1918, the Commissars oversaw the execution of thousands of peasants for resisting. Deserters were combatted by punitive actions against villages thought to be harbouring them - villages might be burned to the ground, or their leaders be shot. This was largely responsible for driving deserters out into the forests, forming the so-called "Greens" of the civil war - they acted as partisans against the Red Army.
The Commissars, always Party members, spread Communist indoctrination throughout the soldiers. The Red Army was on the whole very literate and there were many schools to teach them the ABCs of Communism. After the Civil War, the Commissars and the ones they had indoctrinated would return to their villages and take the military ethos of Bolshevism with them, helping to spread it through the government apparatus in the 1920s.
During Operation Barbarossa in World War II, Commissars had a similar role. This war, which was probably the most audacious invasion of all time, was the conflict of the two ideals which have been most effective in stirring men's hearts - an ideology of race, and an ideology of class. Violence perpetrated against civilians by both sides in this war reached a scale unprecedented (it is estimated it cost the lives of ten million Russian civilians), and conditions for the troops were equally as difficult. As during the Russian Civil War, when over four million troops had deserted from the Red Army by 1921, desertion and mutiny were common problems.
The Commissars of World War II for the Russians were the NKVD. On July 28, 1942 the order was issued that anyone retreating should be shot. NKVD units attached to batallions would stand behind men in an attack and shoot them should they dare retreat (like in the film Enemy at the Gates). Then, on the other hand, they would churn out the Party propaganda designed to raise the spirits of the Red Army soldiers and make them actually want to make the sacrifice for the Socialist Fatherland. Scaremongers or doubters of Party ideology were regarded as especially dangerous and would be dealt with harshly by the Commissars. As during the Russian Civil War, the Commissars had a great deal at stake personally - if they were captured by the enemy's forces, they would be treated the most harshly. Communist Party members were, on Hitler's orders, to be shot.
The Germans' intent to exploit the lack of what Commissars would call "political education" (ie. indoctrination) among Red Army soldiers gave the Commissars cause for concern. Rumours were spread that the Germans allowed deserters in their territory to go home, which encouraged this practice. And so to discourage desertion, persecution of the family's of deserters was institutionalised. This pattern of terror as a means of control of the armies characterised the Eastern Front of World War II and was the source of its brutality. But it can also be argued that this strict regime helped propel the Soviet Union to victory - and the idea of Commissars has spread throughout many Soviet-inspired armies, most notably the People's Liberation Army (China) and the North Vietnamese Army.
1. The famous General Brusilov, commander of the South-Western Front in World War I, decided that "for better or worse, the Russian people had chosen the Bolsheviks" and so it was his duty to defend Russia. Tellingly, he had declared before the Russian February Revolution of 1917 that if he had to pick defending "the Tsar" or "Russia", he would pick "Russia" (many other Generals and aristocrats saw these as one and the same).