One of the main times I struggle in Dota is when I am laning as a melee hero against ranged ones. Whenever I go forward to last hit they harass me a lot, and I lose a lot of health. Is there a good way to combat this?
No matter what: don't die! Your survival is more crucial than your farm; it is more important that you stay in lane soaking XP than it is earning gold. I assume you are mostly referring to being an off-lane (Radiant top lane, Dire bottom lane).
Last hit under tower
Ultimately, you want to keep away from the ranged heroes until you are more familiar with whoever who are playing and confident in your ability. It's best to stay close to your tower, and learn how much damage a tower does to each creep (6 hits for melee, 3 hits for ranged creeps) and learn exactly how to get last hits in between. Normally you'd want to hit each creep once (assuming your creeps aren't attacking it) and then wait for the tower to do 5 or 2 hits (depending whether the creep is melee or ranged) then get the last hit.
Keep creeps at your tower
You can block your own creeps (body block: stand and move in front of your creeps path to slow them down) to keep creeps near your tower
Aggro enemy creeps
As @Rishabh suggests, aggro the enemy creeps and let them follow you behind your own creeps.
Pull neutral camps
You can also pull the jungle creeps into the lane which will drag your creeps away. This is only advisable if the enemy has done the same, and your creeps are pushed up. This can also deny your enemy experience, and net you more CS (creep score) against those neutrals. Since your creeps will have less health or there will be fewer of them, the enemy creeps will make quicker work of them.
Which comes with its own risks
Be aware this comes with its own risks: your enemies may be able to push down the tower quicker which is not good news (whole team gets XP and gold, plus they gain map control). It's probably best to keep the enemies off your tower and forsake gold
Buy sustainability. Don't worry about getting a Bracer or Null Talisman, focus on getting a Stout Shield, Tangos (buy 8 if you want!), health pots, Mangoes and Faerie Fires. It is far better to stay in lane than it is to have to leave. Leaving lane forces you to lose XP and gives your enemies uncontested farm
Kill neutral camps
It may be possible, if the enemy is pulling neutral camps into their lane and denying you the creeps, to kill the neutral camps instead. This will depend on who you are playing, but remember you don't want to lose too much health from doing this. It may be best to just stand within range of your creeps (XP range is 1300 as of 6.79 as @Joe points out in the comments)
Your death is very detrimental. Understand that it is far more important to go without gold from creeps than it is to give a death to your enemies. Doing so subsequently leads to other actions:
- You lose out on any creeps that would've been safe to last hit, plus you miss out on all the XP. You lose gold from dying, and your enemies gain gold and XP
- the enemy carry that killed you gets gold for items and XP plus uncontested space to farm; or
- that support that killed you can now afford more wards or smoke
and an upgraded courier* which now benefits the rest of their team. They gain experience and can now start roaming to other lanes for successful ganks
If you are going to take too much damage for the sake of a last hit: leave it! It will be very frustrating laning against a duo who deny all creeps etc, but dying is far far worse! You want to stay away from dying, so play safe even if you lose last hits!
Matchmaking sometimes takes 10 minutes to find a match. During this time, you could browse the internet, read guides or watch YouTube videos, or you could practise your last hits in the Demo Hero functionality (select Heroes, choose a Hero then select Demo Hero). Once your match is ready, it will alert you and you can select Accept to go into the match.
Note that as of the spring cleaning patch, a new training mode Last Hit Practice has been introduced for this exact purpose. This mode is better than the demo hero mode, as it focusses specifically on last hits (and denies).
Ranged heroes almost always have an advantage over melee, except if they are in melee range, so for the most part you have to sort of deal with it and try not to die. A few things that can help though are trying to last hit only when they have just used an auto attack or ability, and won't be able to punish you for moving into their range. Also, if you are 1v1, consider moving into melee range and dueling with them and getting them low enough they have to back off. This is easier if you have a gap closer, but also risky if they are able to kite back, are crowd control you in some way. If you are 1v2 as melee vs double ranged, then you will probably just have to play safe until a teammate comes and helps.
I wanted to add up something.
Know your role and hero
The other important thing is what your role in game and which lane you are standing. And it's important to know your hero advantages. For example, if you are Bristleback on the hardlane soloing against two range heroes you can go for stealing some creeps and getting experience or even outplay your enemies with juking in trees and using Quil Spray(don't sure about spelling). But there is other way if they are too strong for you or they come even with three heroes on you during first two minutes. You can go stack ancients or other camps. It's easy to farm them out later and get lost XP and gold.
PUSH THE LANE (sometimes)
Here is another example of situation. Let it be safelane and you are farming against Lich and Faceless Void. In this kind of situation Body block is nice, but not enough against Lich. If your teammates don't want or just can't pool any camp(they maybe blocked), you will be struggle near enemy tower, but tower won't reach creeps(just because enemies can deny enough for this). Obviously, this position is not good for you - you can be harassed by Lich with Frost Nova and even can be ganked from river. So you should auto attack creeps and turn on your Ring of Basilius(if you have it), or even cast spells to push lane harder. The point is that your creeps will die under their tower and enemy creeps will go further from their tower. In such situation you obviously can't outzone enemies from XP but you can tilt their position a bit with such moves so that lane won't be stable.
Help your team in hopeless situations or cooperate to clean the lane
In cases when you are already lost lane and they got some strong range hero that can easily kill you - Lina, Necrophos, etc..