Why do spaceships heat up when entering earth but not when exiting? Recently I read up on spacecrafts entering earth using a heat shield.  However, when exiting the Earth's atmosphere, it does not heat up, so it does not need a heat shield at that point of time yet.  Why is this so?  I know then when entering earth, the spacecraft will heat up due to various forces like gravity, drag and friction acting upon it, thus causing it to heat up.  This is the reason why a spacecraft entering Earth's atmosphere would need a heat shield.  Why wouldn't an exiting spacecraft experience this too?  Any help would be appreciated.
 A: On launch, the change in velocity is provided by the rocket engines. As the rocket flies, it is throwing away mass in the form of rocket exhaust -- typically more than 90% of the initial mass of the rocket is propellant. Because thrust is remaining nearly constant while mass is decreasing, acceleration increases over the course of the launch¹, so much of the speed increase occurs late in flight, when the rocket is outside of the densest part of the atmosphere, so much less compression heat is generated (though David Hammen is correct that the payload fairing does require significant attention to thermal design). The acceleration to orbital speed occurs over a fairly long period of time - typically 10 to 15 minutes depending on the design of the launcher.
On reentry, the change is velocity is provided by air resistance; this obviously can't occur until the re-entering spacecraft is in relatively dense atmosphere. Once it begins to decelerate significantly, there's a positive feedback effect; as the craft's horizontal velocity decreases, it loses altitude more rapidly², bringing it into denser air, which decelerates it still more rapidly. Because of this, the vast majority of the deceleration occurs over a very short period of time, about two minutes. All the kinetic energy associated with orbital velocity gets converted to heat in that period.  
¹ Most real rockets are multistage, which complicates this, but it's still true to rough approximation.
² Complicated in real-world craft by lift effects, which cancel out some of the altitude loss or even reverse it in skip-entry trajectories, allowing the reentry phase to be extended in time, reducing the g-force on the crew and peak temperature of the airframe, but extending the total duration of heating and stress.
A: 
Recently I read up on spacecrafts entering earth using a heat shield. However, when exiting the earth atmosphere, it does not heat up, so it does not need a heat shield. Why is this so?

A spacecraft on launch does heat up, just not to the degree that it does on reentry. And it heats up for the same reason--atmospheric drag, which includes adiabatic air compression and atmospheric friction. The key difference between launch and reentry is that they are two different flight profiles meant to optimize the drag variable (less drag on launch, more drag on reentry). (This is a simplified statement to address the OP's question regarding vehicle heating--real rocket launch and reentry dynamics are multi-variable optimizations.)
On launch the rocket spends the initial portion of flight attempting to gain altitude to go into the upper atmosphere where the air is less dense. Then it switches into a lateral velocity regime to gain the necessary lateral velocity to obtain orbit. The rocket profile is attempting to minimize drag as it is a waste of fuel. Less drag = less heating.
Look at the launch profile below. You see the initial moments of the launch the rocket does not move downrange much, relative to its altitude. It is in the later portions of flight that it begins to travel laterally once it has punched out of the dense, lower portion of the atmosphere. You can even see that the maximum aerodynamic forces, Max-Q (drag), are experienced very low in the atmosphere, mostly because of the density of the air.


I know then when entering earth, the spacecraft will heat up due to various forces like gravity and drag and friction acting upon it, thus causing it to heat up.

On reentry the flight profile is optimized to experience increased drag while maintaining a survivable level of deceleration and thermal load. They do this because the vehicle needs to shed orbital velocity (on the order of 16,000 mph) and the cheapest way to do this is to let atmospheric drag slow you down. The technique is called aerobraking. Because they have designed the flight profile to generate increased drag (as compared to launch) and because the velocity with which it penetrates the atmosphere, it experiences much greater heat build up than on launch. More drag, more speed = more heating. 
The generated heat simply comes from the conservation of energy. The vehicle's velocity is shed as heat via ablation (of the reentry shield), adiabatic air compression, and other effects. The kinetic energy of the vehicle is transformed into thermal energy, resulting in the loss of velocity. Just like in your car, when it comes to a stop, the brakes will have become very hot because they have converted the KE of the vehicle into thermal energy.
Now look at the reentry profiles below. You notice that they have a near level part in the middle. That is where the aerobraking maneuver is performed.


If they did not use aerobraking, then the vehicle would have to carry enough rocket fuel to fire against the direction of motion until the relative velocity was sufficiently slow to come down without heating and/or vehicle disintegration. So this method of landing, without aerobraking is possible (its how we land on airless moons), but extremely inefficient.
A: There is theoretically absolutely no need to heat up a spacecraft.
Essentially we can move the spacecraft like a feather into orbit, vertically up and down...theoretically. The other answers do not say this explicitly.
But there is a very ugly problem for engineers, the Tsiolkovsky rocket equation and the very deep gravity well of the earth.
$v_e$ is limited by the propellants we are using. We are really using nearly optimal chemical propellants with hydrogen/oxygen (kerosene for the lowest stage), so no real optimization possible.
$ln \frac{m0}{mf}$ is also optimized as far as possible, rockets are stripped down to the absolute bare minimum, but a ratio of 10:1 is bordering on technical limits.
Despite every optimization this is still not enough to leave Earth.
So we need several stages to achieve orbit. So we can get finally out of the Earth, but...how do we get back? We would need fuel to slow us down again, but we haven't really fuel to spare.
So the engineers decided to use atmospheric entry to slow down the spaceship with a heat shield. A softer method is aerobraking to reduce the speed with several passes through the atmosphere. If we would have a torchship that does not work with the rocket limitations, that would be a real nice thing because we wouldn't need the dangerous and unnecessary reentry phase.
A: While it's already been correctly answered, a suggestion to get a better picture of it:  The game Kerbal Space Program.  While it certainly isn't a perfect simulation of space flight it's good enough to give you a pretty good idea of most of it.
Turn too early and your rocket overheats and blows itself to bits.  Even flying what MechJeb (a very popular mod) says is an optimum trajectory you get an appreciable amount of heating as it goes horizontal in the fringes of the atmosphere.
While this might seem wasteful some experimenting with launching the same rocket over and over with different parameters shows that the heating costs you less fuel than climbing higher first does.  The smooth front of the rocket is a big factor here--if you're trying to fly some abomination that doesn't present a smooth face to the airstream (unfolding is only effective at the level of individual parts.  Combine that with needing a large wheelbase to make a reasonably stable rover on low-g worlds and you can end up with rovers you can't get in a fairing) you need to go farther out before you turn.
A: Spacecraft do indeed heat up as they leave the atmosphere.  They suffer aerodynamic heating just like everything else.  However, there is a major different: direction.  As you are accelerating upwards, you are traveling through thinner and thinner atmosphere, faster and faster.  These partially cancel each other out, keeping your heating reasonable.  On the way down, you are traveling into thicker and thicker atmosphere, and must dissipate the heat as you go.
If you were, say, fired from a railgun, you'd experience the greatest heating at the start, where you are going very fast at low altitudes (thick atmosphere).
If you feel the reentry should be more symmetric with the launch in terms of heating, consider this: on the bottom of the rocket being launched is a great big ball of angry fire that is at least as hot as the reentry. 
A: Velocity and efficiency.
An object trying to get into orbit will travel in a pretty steep parabola.  The longer you spend in the atmosphere the more energy you lose to drag, and the more you lose to drag the more fuel you need.  So a solid strategy for achieving orbit is to get to your target orbit with a minimal curve and then burn until you have the right lateral velocity.  Part of the reason for this is that increasing your orbital velocity affects your altitude 180 degrees away, on the opposite side of your orbit.
An object that is deorbiting will be losing velocity (urg, see edit note 1) and you generally want to use the atmosphere to help you brake, since fuel for braking is the most expensive fuel on the trip.  That means you're entering the atmosphere with a lot of your orbital velocity left, and you need at least 8km/s to stay in a low orbit.  When you're travelling that fast the air simply can't get out of your way quickly enough, and any time you compress something you also heat it up.
Or if you want a simpler answer: Heating up due to atmosphere costs you energy, you want to avoid that as much as possible when going up and take advantage of it when coming back down.
Sorry if this answer sounds disjointed.  https://what-if.xkcd.com/58/ goes in to a lot more detail than I can here and with considerably better authority than I have on the subject.
You might also want to have a read through of https://what-if.xkcd.com/24/ and https://what-if.xkcd.com/28/ for further information on launch and re-entry profiles respectively.

Edit Note 1:  I suppose I should be clearer on this... an object trying to deorbit is trying to lose velocity but it's not accurate to say it is decelerating the whole time.
During the first part of a deorbit the object is decreasing its acceleration while its velocity is increasing, it doesn't start properly decelerating until it's fairly suborbital.  That's probably going to be around the point where aerobraking is doing its job though, somewhere in the area of 40-60km up.  Exactly where the peak velocity is depends on a lot of things, including the object's terminal velocity and how much fuel you have to use up.
The point I was trying, badly, to make is that an object that wants to deorbit also wants to lose velocity to make that happen in a less destructive way.
A: Aerodynamic heating depends on how dense the atmosphere is and how fast you are moving through it; dense air and high speed mean more heating. When the rocket is launched, it starts from zero velocity in that portion of the atmosphere which is densest and accelerates into progressively less dense air; so during the launch profile the amount of atmospheric heating is small. Upon re-entry, it is descending into the atmosphere starting not at zero velocity but at its orbital velocity, and as it falls towards the earth it is picking up speed as the radius of its orbit decreases. By the time it runs into air dense enough to cause heating it is moving at tremendous speed and it gets very, very hot. 
A: When an object orbiting the earth enters the descending path of re-entry it has huge speed, hence huge kinetic energy and it also has potential energy of approximately m.g.h. Because 100 km is a fraction of 6.7 k kilometer radius of earth we can assume the potential energy as above equation . For an orbit of 100 kilometers altitude this speed is approximately 8 km/s. 
So the spaceship's energy E =1/2m(orbital mass)*V^2+ m.g.100 km 
And almost all of this energy, minus the small speeds in the order of 0.1km/s when the parachutes are deployed, must be dissipated by friction of earth atmosphere! To make the matters worse the density of atmosphere is not going to be significant until a very thin strata of air starts at about 50km altitude and gradually increases to sea level. This huge friction on the heat shield of the space-ship over a very short period of time creates extreme heat and very high temperatures!
However during the lift off and climb the rocket and spaceship are initially traveling through dense strata of air  very slowly and as speed increases the air is thinning inversely, hence the friction is kept to tolerable levels!
A: I dont think that anybody has yet mentioned the great impotence of aerodynamic lift. The space shuttle is a winged vehicle that can glide, and even though its lift/drag ratio is very small (less than 1.0) it can achieve a very flat glide trajectory as it decelerates. In this way it can burn off a lot of its speed while still in the upper part of the atmosphere and be traveling much slower when it hits the denser air. Rentry without lift is called ballistic. It creates very much larger g-forces and heating rates.
A: The spacecraft taking off is already outside the stratosphere when it has the speed that re-entry spacecraft posses when they enter the stratosphere.  The stratosphere only extends for about 100 miles above sea level.
Since a rocket takes off vertically it will clear the stratosphere in less than 8 minutes and way before it has the speed to cause any appreciable friction in said part of atmosphere.
The re-entry spacecraft, on the other hand, is using the atmosphere to slow down from orbital velocity.  It needs to slow from 8 to 10 km/sec to a much slower speed in which to either deploy parachute or land on an extended runway.  This is a very significant reduction in speed and the friction in the atmosphere is what accomplishes this reduction.  Since friction causes heat and it will have to spend considerable time in atmosphere to work off the speed, an evaporative tile heat shield is necessary. 
