The rules of D&D 5e do not specify requirements on alignment or deity; the two do not need to share any relationship to one another. Clerics can only get domains offered by their deity, but other classes are not affected by their choice of deity in any way. And alignment is even freer than that—not even clerics are affected mechanically by their alignment. Some items and effects may change somewhat depending on the user’s alignment, but that’s as far as it goes (and those are pretty rare).
However, it may be worth noting that traditionally (read: previous editions), the Forgotten Realms, the default setting for many D&D 5e products, had fairly stringent requirements. Mortals were required to have a patron deity (though they may offer prayers to other deities who cover a particular challenge). Being “Faithless” could cause a lot of trouble for a person, and should one die in that state, one’s afterlife is particularly grim: the soul is added to the Wall of the Faithless.
And generally speaking, true faith in a Realms deity required an alignment that is at most “one step” removed from the deity’s: an LG deity had followers who were LG, NG (L→N is the step), or LN (G→N), but not anything else (LE would be G→N→E, two steps; TN would be L→N and G→N so also two steps; etc. for remaining alignments).
But other settings always do things differently. For example, in the third edition, the Realms were not the default, and instead by default only ordained priests (usually clerics) of the deity have to be close in alignment, while lay followers can stray further. In the setting Eberron, like 5e does by default, even clerics could have completely different alignments (and secretly-evil clerics of nominally-good faiths like the Church of the Silver Flame were a major part of the world and plot).
The effect of faith and faithlessness also varied significantly: by default, even clerics could have no deity and instead worship an “ideal.” In Eberron, it is difficult to tell if gods even exist and faith vs. atheism has little demonstrable effect (barring the personal comfort that faith can bring the faithful). And the Dark Sun setting basically doesn’t have deities at all; clerics there worship the elements.
So you see how all of this can be changed by the DM to tailor the world to the particular game. You can play in the Forgotten Realms with less stringent faith requirements, as in 5e as written (but you should know that a lot may change in the cosmology as a result; you may want to look into how much Wizards has written about the change), you can play Eberron with more present and active (and picky) deities (but you should know that this changes the tone of the setting in some significant ways). And obviously, if you are playing in your own setting, you can do whatever you want.
The ranger class is also somewhat interesting: the nature magic of rangers is something they share, to an extent, with druids. Often these classes were said to worship “nature” more than any particular deity (again, with the exception of the Realms, where deity-worship was compulsory; there druids and rangers both tended to worship particular deities of nature or natural phenomena rather than nature as a concept, and their magics come from that deity rather than just being natural).