Danzo sent both Kakashi and Rin out on a mission. Rin had a jujitsu that was vital to the war, however she needed to pass it along to the leafs ally. After Danzo sent both of them out he held Kakashi back to tell him the "secondary" part of the mission. That part was to kill Rin if it looked like they would be captured by the enemy, he was to kill her. Since this was the stage of his life where he believed that the "Mission was everything" he went along with it.
However, this wasn't the true purpose of the mission. The purpose was to test Kakashi to see if he would be able to kill his best friend to prove himself so that he could become an ANBU. After sending both of them out, Danzo leaked information to the enemy so that they would be ambushed, forcing Kakashi into the situation Danzo wanted.
Sadly, Kakashi followed through in killing his best friend for the good of the village. At this point Obito walks in and catches the act.
Second theory is that Madara put a genjutsu on Kakashi, setting the entire situation up.