The problem is that you're not taking into account "the relativity of simultaneity", i.e. the fact that clocks which are synchronized in one reference frame will appear out-of-sync in another.
Here's a simple example. Suppose, for the sake of making the math a bit easier, that we measure distance in units of "fivers", where a fiver is defined to be the distance light travels in 0.2 seconds, so that light is defined to have a velocity of 5 fivers/second. Suppose you see a ruler which is moving at a velocity of 3 fivers/second along your x-axis. In its own rest frame, this ruler is 40 fivers long; so in your frame its length will appear to be:
40*squareroot(1 - v^2/c^2) = 40*squareroot(1 - 9/25) = 40*0.8 = 32 fivers long. Also, at either end of this ruler is placed a clock; using the time dilation formula, we can see that for every second on your clock, you will only see these clocks ticking 0.8 seconds forward.
Now, say that when t=0 according to your clock, the clock on the left end of the ruler also reads t'=0. At that moment, a light is flashed on at the left end of the ruler, and you observe how long the light pulse takes to catch up with the right end. In your frame, the position of the light pulse along the x-axis at time t will be c*t, while the position of the right end of the ruler at time t will be v*t + 32. So, the light will catch up to the right end when c*t = v*t + 32 which if you solve for t means t = 32/(c - v). Plugging in c = 5 and v = 3, you get a time of 16 seconds, in your frame.
Now the key to understanding how the ruler can also measure this pulse to be moving at c is to realize that different frames have different definitions of what it means for a pair of clocks to be "synchronized". In your frame, when the clock on the left reads t'=0, the clock on the right will
not read t'=0; in your frame, the clock on the right is always 4.8 seconds behind the clock on the left, so it will read t'=-4.8. This means that after 16 seconds have passed according to your clocks, only 16*0.8 = 12.8 seconds will have elapsed on the ruler's clocks, which means the clock on the right reads -4.8 + 12.8 = 8 at the moment that the light reaches the right end. So remembering that light was emitted when the clock on the left read t'=0, the light must have taken 8 seconds to cross the ruler in the ruler's own frame; and remembering that the ruler is 40 fivers long in its own frame, the speed of the light pulse is measured to be 40/8 = 5 fivers/second. So, light does indeed have the same speed in both frames.
The general equations for converting between coordinates (x,y,z,t) in one frame S and coordinates (x',y',z',t') in another frame S', where S measures the origin of S' to be moving at velocity v along the x-axis, are:
x' = gamma*(x - v*t)
y' = y
z' = z
t' = gamma*(t - v*x/c^2)
and
x = gamma*(x' + v*t')
y = y'
z = z'
t = gamma*(t' + v*x'/c^2)
where gamma = 1/squareroot(1 - v^2/c^2).
These are known as the "Lorentz transformation" equations. You can verify that these equations work for the example above--for example, in my coordinates, the light hit the right end of the ruler at t = 16 and position x = 5*16 = 80. So with v=3 and c=5, gamma will be 1.25, so the equations say that in the ruler's coordinates, this event will happen at:
x' = 1.25*(80 - 3*16) = 40 (the ruler is 40 fivers long in its own frame)
t' = 1.25*(16 - 3*80/25) = 8
which is what I got earlier. I should note that the logic is actually a bit backwards here--in relativity the fact that light moves at the same velocity in all frames isn't really a result but rather a starting assumption, you say that each observer should assign coordinates to events using a grid of rulers and clocks which are at rest relative to himself, and you synchronize clocks at different locations using the
assumption that light travels at c in all directions relative to the observer (so that if light is emitted at the midpoint of two clocks, the clocks should both have the same reading at the moment the light hits them). If different observers define coordinates in this way,
and if the laws of physics have the same form in each observer's coordinate system (this last part is really the empirical content of the theory of relativity), then you can show that the Lorentz transformation equations must be the correct way to translate between different observers' coordinate systems.
You might also want to check out my post
An illustration of relativity with rulers and clocks which gives a more detailed example of how a series of clocks which are in-sync in one frame will appear out-of-sync in the other, and vice versa; this is key to understanding some other apparent paradoxes of relativity, like how each observer can think it's the
other observer's clocks which are running slower than his own.