This is a very common question by athletes but the truth of the matter is that you will not lose speed if you gain the right weight. If you gain the wrong weight that is a different story.
Speed comes from your fast twitch muscle fibers. Those are the same fibers you work when you lift in the weight room heavy and for low reps. If you are eating properly and training to put on weight, your speed will not deteriorate it will actually increase if anything. It is all the same. Speed is a maximum effort movement just like lifting heavy in the weight room. It takes your max effort and your fast twitch fibers.
You are more likely to lose speed if you are lifting light weight for a lot of reps. It is making you less explosive. Now if you are eating a lot of processed foods and bad carbs you will be putting on bad weight. That is what will make you slower. But if you are eating a high protein diet with good carbs, you will not lose speed.