Okay, so there are two types of barrel in an Airsoft gun. The outer barrel and the inner barrel. Both are relatively easy to change.
You don't have to change the length of the inner barrel if you don't want to/outside your budget for now but it's recommended. Especially if there is a large difference in size between your current barrel length and new barrel length. An inch or so difference, I'd say doesn't matter too much, any more and you'll have potential performance issues if your BB is being propelled with enough air to get it out of a X length barrel but your barrel is X+Y in length now.
Ideally you want to have the air cylinder have enough air volume inside it to be able to completely displace all the air in your inner barrel. This means the BB is being pushed along the inner barrel for its entire length instead of only partially before having to rely on its down moment to over come air resistance.
If you do change the inner barrel be sure to make sure your air/piston cylinder is ported to match your new inner barrel length for proper air voluming.
The outer barrel is the barrel that you can actually see when you're look at a Airsoft gun. Changing this is straight forward.
You simply remove your front rail and slide the outer barrel forward and then just slide the replacement in its place. Most airsoft guns (especially m4's) use the same style of outer barrel so any replacement should just slide in with the short guide rails lining up with the receiver guide slots.
The inner barrel is easy to change but has extra steps involved.
To change the inner barrel, remove the upper receiver from the lower, then slide the hopunit with attached inner barrel out the back.
Set the hopunit to zero hop (crucial to avoid tearing hop rubber). You can tell when you've got zero hop when you look down the inner barrel and can see no protrusion into the channel from the hop rubber.
On the hop unit you'll see a little c clamp/clip on the under side of the tube the inner barrel is in. Pop this clip off and slowly slide your inner barrel out. The hop rubber will most likely come off with the barrel. If it doesn't, gently pull it out after you've removed the inner barrel.
Place the hop rubber on your new barrel (hop rubbers have a little ridge of rubber that's designed to fit in a small channel on the inner barrel to ensure the hop rubber is correctly rotated on the barrel).
Slide the inner barrel into the hop chamber again and clip the c clip/clamp back into place. Insert the hop unit back into the reciver.
Viola, you've just changed your outer and inner barrels to go with your new front rail.
Hope this helps!