Ali was the caliph between 656 and 661 CE, one of the hardest periods in Muslim history, coinciding with the first Muslim civil war. He reigned over the Rashidun empire which extended from Central Asia in the east to North Africa in the west. He became known as a both just and fair ruler.
Selection as Caliph
After the assassination of the third Caliph, Uthman Ibn Affan, at the close of the Siege of Uthman, the selection of a new Caliph encountered difficulties. The rebels, comprising Muhajerin, Ansar, Egyptians, Kufans and Basntes, and the Kharijites, were divided between three candidates: Ali, Talhah and Al-Zubayr. First they referred to Ali and asked him to accept the caliphate. Also some Sahaba tried to persuade him to accept the office. But he refused and answered:'Leave me and seek someone else. We are facing a matter which has (several) faces and colors, which neither hearts can stand nor intelligence can accept. Clouds are hovering over the sky, and faces are not discernible. You should know that if I respond to you I would lead you as I know and would not care about whatever one may say or abuse. If you leave me then I am the same as you are. It is possible I would listen to and obey whomever you make in charge of your affairs. I am better for you as a counselor than as chief.'
Then rebels offered the caliphate to Talhah and Al-Zubayr and some other companions but they refused it too. Therefore, they threatened that, unless the people of Medina choose a caliph within one day, they would be forced to take some drastic action. In order to resolve the deadlock all of the Muslims gathered in Mosque of Prophet on 18 June 656CE. (25th Dhu al-Hijjah 35AH.) to choose the caliph. Ali refused to accept caliphate by the fact that the people who pressed him hardest were the rebels, and he therefore declined at first. When the notable companions of Muhammad as well as people who live in Medina urged him, however, he finally agreed. According to Abu Mekhnaf's narration Talhah was the first prominent companion who gave his pledge but the other narrations claim they didn't do so or even somebody forced them to do so. However he and Al-Zubayr later claimed they did so reluctantly, but Ali refused this claim and said that they did so voluntarily. Mudelong believe that force was not used to urge people to give their pledge and they pledged in public in the mosque. 
While the overwhelming majority of people who lived in Medina as well as rebels gave their pledge, some major figures did not. Umayyads, kins of Uthman, escaped to Levant or remained in their houses and later refused Ali's legitimacy. Sa`ad ibn Abi Waqqas were absent and Abdullah ibn Umar abstained from offering his allegiance but both of them assured Ali that they wouldn't do anything against Ali.  Another prominent figure who was in Mecca at that time and later opposed Ali, was A'isha, Muhammad's widow.
Reign as Caliph
At the beginning Ali told people that Muslim polity had come to be plagued by dissension and discord and he wanted to purge Islam of all evil from which it had come to suffer. Then warned all concerned that he would tolerate no sedition and all found guilty of subversive activities would be dealt with harshly. He advised people to behave as true Muslims.
But he soon found that he was helpless and the prisoner of the people who didn't obey him. The caliphate had come to him as the gift of the rebels and he didn't have enough force to control or punish them. When some people asked Ali to punish those who killed Uthman, Ali answered, "How do I have the power for it while those who assaulted him are in the height of their power. They have superiority over us, not we over them." While A'isha, Talhah, Al-Zubayr and Umayyad especially Muawiyah I wanted to take revenge for Uthman's death and punish the rioters who had killed him. However some historians believe that they use this issue to seek their political ambitions due to finding Ali's caliphate against their own benefit.
Soon after Ali became caliph, he dismissed provincial governors who had been appointed by Uthman, and replaced them with trusted aides. He acted against the counsel of Mughrah ibn Shobah and Ibn Abbas, who had advised him to proceed cautiously. Madelung says Ali was deeply convinced of his right and his religious mission, unwilling to compromise his principles for the sake of political expediencey, ready to fight against overwhelming odds. Muawiyah, kinsman of Uthman and governor of Levant refused to submit to Ali's orders - the only governor to do this.
After the Battle of Bassorah Ali transferred his capital from Medina to Kufa, the Muslim garrison city in Iraq. Kufa was in the middle of Islamic land and had strategic position.
Ali resumed the land which had been granted by Uthman and swore to resume whatever some elites had taken before him. He opposed the centralization of capital control over provincial revenues and favored an equal distribution of taxes and booty among the Muslims; in contrast to Umar he distributed the entire revenue of the divan among Muslims without keeping anything in reserve. When asked to pay more money to elites he said "Do you command me that I should seek support by oppressing those over whom I have been placed? By Allah, I won't do so as long as the world goes on, and as long as one star leads another in the sky. Even if it were my property, I would have distributed it equally among them, then why not when the property is that of Allah."
Ali believed that people and governors have rights over each other and God created these rights so as to equate with one another. The greatest of these rights that Allah has made obligatory is the right of the ruler over the ruled and the right of the ruled over the ruler. If the ruled fulfill the rights of the ruler and the ruler fulfills their rights, then right attains the position of honor among them, the ways of religion become established, signs of justice become fixed and the sunnah gains currency. He wrote directions for his officials which clearly show what form of regime he wanted to introduce. It was not to be a regime whose officers had an upper hand and were fattened on public money. It was to be a regime where the governed and the tax-payers were at premium. It was their convenience for which the State was to function. It was a welfare-state working solely for the welfare of the people living under its rule, a regime where the rich cannot get richer while the poor are made poorer; a regime where canons of religion hold the balance between the governed and the ruler. He asked people not to speak with him as they spoke with cruel governors and be honest with him.
Ali had decisive beliefs that he shouldn't start a war with other Muslims but when the enemy started it his army wouldn't retreat unless they wanted to attack again. He ordered his soldiers not to kill who would become injured, or not be able to defend himself, or escape from the battlefield and injuries and wanted his warriors not to injure women.
The First Fitna, 656–661 CE, followed the assassination of the caliph Uthman Ibn Affan, continued during the caliphate of Ali, and was ended, on the whole, by Muawiyah's assumption of the caliphate. This civil war is often called the Fitna, and regretted as the end of the early unity of the Islamic ummah (nation). Ali was first opposed by a faction led by Talhah, Al-Zubayr and Muhammad's wife, Aisha bint Abu Bakr. This group was known as disobedients (Nakithin) by their enemies. First they gathered in Mecca then moved to Basra with the expectation of finding the necessary forces and resources to mobilize people in what is now Iraq. The rebels occupied Basra, killing many people. When Ali asked them for obedience and a pledge of allegiance, they refused. The two parties met at the Battle of Bassorah (Battle of the Camel) in 656, where Ali emerged victorious.
Then he appointed Ibn Abbas governor of Basra and moved his capital to Kufa. Later he was challenged by Muawiyah I, the governor of Levant and the cousin of Uthman, who refused Ali's demands for allegiance and called for revenge for Uthman. Ali opened negotiations with him with the hope of regaining his allegiance but Muawiyah insisted on Levant autonomy under his rule. Muawiyah replied by mobilizing his Levantine supporters and refusing to pay homage to Ali on the pretext that his contingent had not participated in his election. The two armies encamped themselves at Siffin for more than one hundred days, most of the time being spent in negotiations. Although, Ali exchanged several letters with Muawiyah, he was unable to dismiss the latter, nor persuade him to pledge allegiance. Skirmishes between the parties led to the Battle of Siffin in 657. After a week of combat was followed by a violent battle known as laylat al-harir (the night of clamor) the Muawiyah's army were on the point of being routed when Amr ibn al-Aas advised Muawiyah to have his soldiers hoist mushaf (either parchments inscribed with verses of the Qur'an, or complete copies of it) on their spearheads in order to cause disagreement and confusion in Ali's army.
The two armies finally agreed to settle the matter of who should be Caliph by arbitration. The refusal of the largest bloc in Ali's army to fight was the decisive factor in his acceptance of the arbitration. The question as to whether the arbiter would represent Ali or the Kufans caused a further split in Ali's army. Ash'ath ibn Qays and some others who later became the Kharijites rejected Ali's nominees, `Abd Allah ibn `Abbas and Malik al-Ashtar, and insisted on Abu Musa Ash'ari, who was opposed by Ali, since he had earlier prevented people from supporting him. Ali was urged to accept Abu Musa but he never did. Those who preferred Abu Musa went decided to continue with the arbitration anyway. The Kharijites (schismatics), later opposed the decision to choose Abu Musa blaming Ali for his appointment and rebelled and Ali had to fight with them in the Battle of Nahrawan. The arbitration resulted in the dissolution of Ali's coalition and some have opined that this was Muawiyah's intention.
Muawiyah's army invaded and plundered cities, which Ali's governors couldn't prevent and people didn't support him to fight with them. Muawiyah overpowered Egypt, Yemen and other areas.
This civil war created permanent divisions within the Muslim community and Muslims were divided over who had the legitimate right to occupy the caliphate.
On the nineteenth of Ramadan, while Ali was praying in the mosque of Kufa, the Kharijite Abd-al-Rahman ibn Muljam assassinated him with a strike of his poison-coated sword. Ali, wounded by the poisonous sword, lived for two days and died on the 21st of Ramadan in the city of Kufa in 661 CE.
Ali ordered his sons not to kill a group of people. Since the act was performed by a single member of Kharijite group and not all of them, they had to revenge just the murderer. Thus later Hasan fulfilled Qisas and killed ibn Muljam.
In these two days he dictated his will to his household "My advice to you is that you should not consider anyone as a co-worker of the Lord, be firm in your belief that there is One and only One Allah. Do not waste the knowledge given to you by the Muhammad and do not give up and destroy his Sunnah [traditions]. Keep these two pillars of Islam [monotheism and Sunnah of the Muhammad] aloft. If you act according to my advice then you cannot be blamed for damaging or destroying the religion."