The Night's Watch, like everywhere else in Westeros, needs a Maester. The position is an important one for numerous reasons: they are scholars (thus know history), and they are healers (thus know medicine). Those are things that the Nights Watch needs to have.
The Night's Watch had a maester before Sam: Maester Aemon was the Maester of the Night's Watch until his death. So, it seems self-evident that a member of the Night's Watch can become a Maester.
Sam hasn't deserted the Night's Watch anymore than the rangers "desert" when they go ranging in the North, or the Brother's "desert" when they go on recruiting drives in the South. Sam is going to Oldtown to become a Maester and return to the Night's Watch.
Since the Maester's are politically neutral (in theory), Sam's vows to the Citadel should not conflict with his vows to the Watch when he returns. In fact, the Maester's Order and the Night's Watch both swear essentially the same vows: they sever all familial ties, can not inherit land nor marry nor father children, and vow to serve "the realm" regardless of who is in power. So their vows mesh very well together, probably more so than anywhere else in Westeros.