How did Einstein know the speed of light was constant? I often hear the story of how Einstein came up to the conclusion that time would slow down the faster you move, because the speed of light has to remain the same. 
My question is, how did Einstein know that measuring the speed of light wouldn't be affected by the speed at which you are moving. Was this common knowledge already before Einstein published his paper on special relativity? If not, what led him to that conclusion?
 A: I'm sorry but @AWanderingMind answer is incorrect. The definition used for Galilean invariance was wrong (which the whole answer was based on), and it does not resemble what happened. The speed of light from Maxwell's equations was not considered constant in all inertial frames, that's what this whole dilema was about. You can read about this in Griffith's electrodynamics book.
https://en.wikipedia.org/wiki/Special_relativity
Galilean invariance applied to equations of motion, not all laws of physics. Einstein's theory of relativity said that all laws of physics are the same in all inertial frames. He came to this conclusion with the idea of a loop on a train moving through a magnetic field. For the people not on the train, they consider a motional emf is created, with the flux rule,
$ε=- d \Phi /dt$
Whereas the people on the train would use Faraday's law and get the same results, even though their physical explanation for the process is completely different. Through transformations Einstein was able to connect the two explanations to each other. Like he was able to show that a how a moving charge with no magnetic field in its rest frame translate to a magnetic field in the frame it is moving through. This is the new stuff he figured out, not EM wave speed from Maxwell's equation, that was already known. He thus came up with his relativity conclusions, since the speed of light would be generated at the same speed for all (as the wave generated from Maxwell's equations show) when all laws of physics (including electromagnetics) are the same in all inertial frames, not just the laws of motion (which did not apply to EM wave generation). There is no absolute rest system.
The Michaelson-Morley experiment he was supposedly vaguely aware of, and many had conflicting interpretations of it (like one was that the earth dragged the ether with it, thus keeping the ether hypothesis alive). For him theoretical reasons were good enough. No ether was required, thus there was no ether. He looked at the Lorentz transformation equations and said they did reflect physical reality, rather than just being an interesting mathematical outcome. 
A: When using the term 'the speed of light' it is sometimes necessary to make the distinction between its one-way speed and its two-way speed.
The "one-way" speed of light, from a source to a detector, cannot be measured independently of a convention as to how to synchronize the clocks at the source and the detector. What can however be experimentally measured is the round-trip speed (or "two-way" speed of light) from the source to the detector and back again.
Albert Einstein chose a synchronization convention see Einstein synchronization that made the one-way speed equal to the two-way speed. The constancy of the one-way speed in any given inertial frame is the basis of his special theory of relativity.
Experiments that attempted to directly probe the one-way speed of light independent of synchronization have been proposed, but none has succeeded in doing so.
The paper A Comparison between Lorentz's Ether Theory and Special Relativity in the Light of the Experiments of Trouton and Noble by Prof. Michel Janssen provides comprehensive historical and theoretical analysis of development of the theory.
Some thought and references on conventionality of distant simultaneity can be found here
A: In 1887 the Michelson Morley experiment provided evidence that the speed of light was independent of the direction of travel of the observer (in their case, they used the movement of the Earth around the sun).
Morley conducted additional experiments with Dayton Miller from 1902 to 1904 which confirmed the results.
Einstein was aware of this (at least later), but it is not clear (and he himself seems to have been unsure) what influence it had, if any, on his 1905 paper.  On this matter, there is more detail given in the answers to the question linked in the comment above, which supports the idea that Einstein was more influenced by theoretical arguments.
The fact that the experiment yielded a negative result (they were looking for evidence of the "Ether") may have made it less noteworthy at the time. But its influence was indirect in that it did not invalidate the theories that relied on a constant speed of light.
A: Besides Michelson and Morley experimental results, Einstein also considered the theoretical aspects.
It can be derived from Maxwell's equations that the speed at which electromagnetic waves travel is: $c=\left(\epsilon_{0}\mu_{0}\right)^{-1/2}$.
Since light is an electromagnetic wave, that means that the speed of light is equal to the speed of the electromagnetic waves. $\epsilon_{0}$ and $\mu_{0}$ are properties of the vacuum and are constants, so $c$ will also be a constant.
Thus from Maxwell's theory of electromagnetism alone we can already see that the speed of light in vacuum should be constant.
On the other hand, Galilean invariance tells us that the laws of motion have the same form in all inertial frames. There is no special inertial frame (as far as Newton's laws are concerned).
Another key element here is Galilean transformation, which was the tool used for transforming from one inertial frame to another.
It can be easily seen that considering the first two elements to be valid:

*

*Maxwell's theory of electromagnetism - speed of light is constant

*Galilean invariance - the laws of motion have the same form in all inertial frames

means that we can no longer apply the Galilean transformation, because otherwise we will get a contradiction. Thus at least one of these three "key elements" must be wrong.

*

*Maxwell's theory of electromagnetism - speed of light is constant

*Galilean invariance - the laws of motion have the same form in all inertial frames

*Galilean transformation

It turned out that the last one (Galilean transformation) was wrong. Einstein considered the first two correct and built the special theory of relativity. The correct transformation from one inertial frame to another, in the assumption of the validity of the Maxwell's theory and Galilean invariance, turns out to be Lorentz transformation
. It is nice to check that the Lorentz transformation does indeed reduce to the Galilean transformation in the $v\ll c$ limit. That's why, in a sense, Galilean transformation is not wrong, but rather incomplete or a particular case. We can say that Galilean transformation needed to be generalized, and this was acomplished by introducing the invariance of the speed of light and maintaining the Galilean invariance.
How do Maxwell's equations predict that the speed of light is constant
Maxwell's equations in differential form:
$$\tag{1}\nabla\cdot \mathbf{E}=\frac{\rho}{\epsilon_{0}}\label{1}$$
$$\tag{2}\nabla\cdot \mathbf{B}=0\label{2}$$
$$\tag{3}\nabla\times\mathbf{E}=-\frac{\partial \mathbf{B}}{\partial t}\label{3}$$
$$\tag{4}\nabla\times \mathbf{B}=\mu_{0}\mathbf{J}+\mu_{0}\epsilon_{0}\frac{\partial \mathbf{E}}{\partial t}\label{4}$$
We can try to derive a wave equation in vacuum. Since we are considering the vacuum, we do not have charge densities, so equation ($\ref{1}$) becomes: $$\tag{5}\nabla\cdot \mathbf{E}=0\label{5}$$
In vacuum we do not have current densities either, so equation ($\ref{4}$) becomes:
$$\tag{6}\nabla\times \mathbf{B}=\mu_{0}\epsilon_{0}\frac{\partial \mathbf{E}}{\partial t}\label{6}$$
Now if we apply the curl to equation ($\ref{3}$), we get:
$$\tag{7}\nabla\times\left(\nabla\times\mathbf{E}\right)=-\frac{\partial}{\partial t}\left(\nabla\times\mathbf{B}\right)\label{7}$$
We can use vector identity to evaluate the LHS of equation ($\ref{7}$):
$$\tag{8}\nabla\times\left(\nabla\times\mathbf{E}\right)=\nabla\left(\underbrace{\nabla\cdot\mathbf{E}}_{=0}\right)-\nabla^2\mathbf{E}\label{8}$$
$$\tag{9}\nabla\times\left(\nabla\times\mathbf{E}\right)=-\nabla^2\mathbf{E}\label{9}$$
For the RHS of equation ($\ref{7}$), we can replace $\nabla\times\mathbf{B}$ with the expression we have from equation ($\ref{6}$):
$$\tag{10}-\frac{\partial}{\partial t}\left(\nabla\times\mathbf{B}\right)=-\frac{\partial}{\partial t}\left(\mu_{0}\epsilon_{0}\frac{\partial \mathbf{E}}{\partial t}\right)=-\mu_{0}\epsilon_{0}\frac{\partial^2 \mathbf{E}}{\partial t^2}\label{10}$$
Putting all together:
$$\tag{11}-\nabla^2\mathbf{E}=-\mu_{0}\epsilon_{0}\frac{\partial^2 \mathbf{E}}{\partial t^2}\label{11}$$
$$\tag{12}\nabla^2\mathbf{E}-\mu_{0}\epsilon_{0}\frac{\partial^2 \mathbf{E}}{\partial t^2}\label{12}=0$$
The general form of a wave equation is:
$$\tag{13}\nabla^2\mathbf{\Psi}-\frac{1}{v^2}\frac{\partial^2 \mathbf{\Psi}}{\partial t^2}\label{13}=0$$
where $v$ is the velocity of the wave.
Equation ($\ref{12}$) decribes an electromagnetic wave moving with velocity $v=\frac{1}{\sqrt{\epsilon_{0}\mu_{0}}}$. Since light is an electromagnetic wave, that means that light is also propagating at this speed in vacuum. And since both  $\epsilon_{0}$ and $\mu_{0}$ are constant, that means that $\frac{1}{\sqrt{\epsilon_{0}\mu_{0}}}$ is also a constant. Hence light moves at a constant speed in vacuum.
A: Einstein was influenced primarily by the electromagnetic theory developed by several people and culminating in the Maxwell equations, and by the experiments on the speed of light in moving water carried out by Fizeau. 
At the time it was not common knowledge what light would do in general; the interpretation of the electromagnetic theory was much puzzled over. The Michelson Morley experiment was a further piece of evidence, but not a crucial one for Einstein. 
He was certainly struck by the fact that Maxwell's equations suggest a physical interpretation in which light will recede from you no matter how fast you go to try and catch it up. This was not self-evident and not the only way to interpret the equations, but Einstein seems to have realised that it would be a valuable exercise to abandon the type of aether ideas being tried out at the time, and just stick to the notion that light speed is independent of the source, and then push this way of thinking through to its logical conclusions. Upon doing this, he discovered that one can still get a self-consistent set of ideas about space and time, but they are different from the more familiar (Galilean) ones.
A: He didn't. He just wanted to explore how would a Universe that has a speed limit such as the speed of light would behave... His theory of relativity is the answer to that quest. If we ever wanted to see how such a universe behaves, we can look at his theory and find out.
Is our universe just like the one Einstein described? That's a diffrent question that relates to the usefullness of the theory.
