here's my take on it:
your main job as a bass player is to outline the basic harmony and rhythm of a piece
so, putting aside rhythm, your basic harmonic job is generally to give some information about what chord is playing at that moment, and to show when the chords change
ok, there a million times that job isn't necessary, or you can otherwise deviate from it, but that's the core of bass playing regardless of the style of music you're playing: show what's happening with the harmony/chord changes
so, if your guitar player/keyboard player/whatever is playing a C chord and you have to supply a suitable bass line, your first consideration ought to be to choose notes that are from within the chord... most frequently the best note to show the fundamental harmony is the root note... so in a C chord (C, E & G), the starting point is a C in the bass
from the root, any note choice you make takes you further from the basic harmony... you can play any note at all, but some notes are so alien to the prevailing harmonic environment that you can only effectively use them sparingly, as briefly-sounding passing notes
remember that although your basic job as bass player (outline the rhythm, outline the harmony) remains the same regardless of music genre/style, the way you do it (and how thorough you have to be) DOES vary according to style... so if you're playing walking jazz bass, it's very common to be very detailed about the harmonic changes... a good jazz bass player will be able to paint a very accurate picture of the chord changes, whereas if you're playing ACDC-style hard rock, you frequently give less harmonic information and pound out root notes
pounding out root notes is not usually very inspiring for bass players, but it often needs to be done.. we're one component in a machine, and someone has to occasionally step up and do the simple/boring job.. you should never be too proud to play simple if the piece needs it (on the other hand, you have to be able to play frantic 32nd notes if the piece needs that too)
ok.. so over the C chord, you can play what you like, depending on style, but your starting point ought normally be the root of the chord
well, what about the other notes you could possibly play?
our ear judges the 'harmonic relevance' of a note depending on many factors, including:
1. how loud the note is
2. how long the note sounds for
3. how high/low the note is
4. whether it is is in the current chord or not
5. whether it sounds diatonic or not
6. what notes preceded it
7. whether the same note/motif has appeared before, and what relevance it had then
8. whether it falls on a strong (emphasized) or weak (not emphasized) part of the bar
as you can see, many factors are at play in determining how a note is judged by your ear (totally subconsciously)... notes played on strong & weak beats have very different meanings... to use our C chord example, the very strongest part of a bar is usually the first beat... so on the 'one' of a bar, your first consideration should be to play a big fat 'C' note... nice and loud (point 1), long enough for them to get the idea (point 2), nice and fat & low, no piccolo bass notes please (point 3)... you get the idea
on weaker parts of the bar (usually off beats and at places within the beats) you can usually place more unusual notes, since the ear often places less importance on them.. especially if you've just hit a big fat ROOT on the ONE...
so, no you don't have to just play notes from the basic triad of a chord, because you have all these little gaps to put other notes
now... since chords normally change throughout a piece of music, we come on to the second part... the bass must indicate the chord changes... so if your C chord is followed by an Am chord, your line has to describe that chord change... some very simple options are: you can pound out root notes through the chords, or you can play arpeggios, or you can play a scalar run that connects the chords, or you can suggest movement chromatically, you can play a line in counterpoint to the melody or another instrument, or you can do a combination of all of these things... the fact that you have strong and weak places to play different notes means that there is a lot of scope to play virtually any note you like provided there is a sense that the note fits into the 'importance hierarchy' discussed above... this is what people mean when they say you can play any note and make it work.. you CAN, but conditions have to be right.. you can't just hit a Gb on the 'one' of a C major chord and expect to make it work somehow... there are very few times when that'll work
by the way, you do not THINK consciously about any of these things while playing... you just play a lot, listen a lot, and play a lot some more... just remember what your job in the ensemble is, and it'll all fall into place