The actual challenge in your question isn't just to prevent the trait from spreading via natural selection - it's also to prevent the trait from going extinct via random drift.
Natural selection is an important factor in evolution, but before you have natural selection you have genetic drift - the natural random fluctuation of how much of a gene there is in a population. Every generation, people with a certain allele have more or less children and pass on the gene to more or less of them, meaning the percentage of people in the population with this allele will go up and down semi-randomly over the generations. It so happens that mathematically, if all alleles (alternate versions of a gene) are equally likely to be passed on to the next generation, after enough generations you will get to a point where there is only one allele left. Simply because when your percentage fluctuates randomly over time, you'll eventually hit "0%" or "100%", and once you've hit either of those numbers you'll stay there (obviously if no individual in the population has an allele they can't pass it on to their offspring). This is called "fixation", when an allele reaches 100% of the population.
The odds of an allele reaching fixation, in the completely random scenario, are proportional to its frequency in the population; meaning the higher the allele's percentage in the population, the higher its odds of taking over the population. Conversely, the lower its percentage, the higher its odds of going extinct. Natural selection doesn't prevent this, it just nudges the odds on whether an allele will reach fixation or go extinct.
So the problem you have with your genetic magic ability is that you want your population to be a stable minority over the long term. Most of the suggestions you'll get for preventing natural selection from promoting the allele may indeed prevent the allele from becoming fixed in the population - but in doing so they'll instead guarantee the allele will go extinct over the long term.
What you need is active selection pressure to keep the frequency of your allele at a low but nonzero share of the population. There are many ways of achieving this; the basic idea is to have some way in which the gene is beneficial (selected upon) if it's rare, but harmful (selected against) if it's common. The best suggestion in the answers so far is the sickle cell anaemia analogy, though they get the mechanism wrong. It's not that the sickle cell alleles protect from malaria at the expense of ill health; it's that if you have one sickle cell allele you're protected from malaria, but if you have two alleles you get sickle cell anaemia. This means that as long as the allele is rare it's going to be beneficial, because most people will get only one copy. But if it becomes too common then you're more likely to get children who have both, which is bad, which means having the allele becomes deleterious.
You could get some interesting side effects if you went with this exact mechanism. If you have a gene with one allele that gives magical powers, and people who are heterozygous for it are magic users, people who are homozygous for the magic allele are reproductive dead-ends (they're sick, they're insane, they're infertile, they die in utero...), and people with any other version of the gene are nonmagical, then that means every magical family will include nonmagical members (because every magical person necessarily has a non-magical allele to pass on). You would also have negative consequences for magical people who have children with each other, which would discourage "inbreeding" within magical communities.
Of course you don't need to resort to complex genetic effects like this, you could have any mechanism that depends on how many magic users there are. For example, social forces could make having magic beneficial as long as they were rare enough to pass under the radar, but if there were too many magic users the witch hunts would start (though I don't think it's that realistic to assume social forces will be constant on evolutionary timescales). Or there could be properties of magic itself - The Dark Ones seek out magic users to eat them, but only wake if there are a lot of people doing magic in the area. Or you have a tradeoff in how sensitive you are to the magical fields: if you're a little sensitive you can do cool stuff, if you're very sensitive you get incapacitating headaches, and the more people are around doing magic the stronger the magical fields become; this would mean when few people have magical powers the threshold for sensitivity is high, and any magic gene is beneficial; if many people have powers the strong magical fields mean it takes very little sensitivity to get debilitating headaches, so the magic genes become harmful, and you end up with a stable situation with a small proportion of people who have the genes, and those come in a range of sensitivities, most being average and some unlucky souls getting the headaches (and maybe those need to move away from the other magic users, living as hermits or in places magical people are rare).
Note that this is all assuming alleles don't appear, which in the real world they do of course via mutation. Whether this is worth taking into account or not depends on how likely and common a given mutation is. It is perfectly reasonable to assume the magic genes are a rare mutation that happened only once or a few times, so you only get a magic gene if you inherited it. But you could tweak things a lot if your magic genes are likely to appear via random mutation; you could even go to the point, as another answer suggested, that magic users are all de novo mutations and they don't spread beyond that baseline mutation probability because they're all sterile... But at that point why have it be a genetic trait in the first place. Point being, if you allow a baseline rate of new magic mutations you have a guaranteed minimum percentage of magic people without having to worry about them becoming extinct; you also have different social dynamics with respect to whether magic is a family thing and how much of a family thing it is.
ETA: I want to note one thing on the mechanism though - you don't have to worry about this if you want to be fuzzy on the whole thing, but if you want natural selection to function in a realistic way you need to keep in mind it acts on individuals. So when talking about whether the magic allele is beneficial or harmful, it isn't enough that it's beneficial or harmful to society, or to the magical community - it would need to be directly beneficial or harmful to the individual that has the allele (even more specifically, it would need to affect how many offspring and grand-offspring that individual can be expected to have).