Internally, the game tracks whether fruit came from a foraging or non-foraging source, and uses this internal information to determine whether or not the fruit should get the +10% Tiller profession bonus when sold.
Sources that are considered to be foraging are:
Wild-grown fruit that randomly spawns throughout Stardew Valley, as detailed at Foraged Items and Spawning
Fruit grown from any of the four varieties of Wild Seeds
If the player has both foraged and non-foraged fruit of the same quality, stacking the items together always results in non-foraged fruit: the Tiller bonus will apply to the entire stack of fruit.
↑The tiller bonus does not apply to fruit where the isSpawnedObject flag is set, see code in Object::getPriceAfterMultipliers. This flag is added to wild foraged items in GameLocation::dropObject and GameLocation:spawnObjects; to crops grown from wild seeds in Crop::newDay; and, to Farm Cave fruit in FarmCave::dayUpdate. Other sources do not get the flag, including notably berries from bushes in Bush::shake.
↑The code in Object:addToStack was changed in v1.4 to ensure isSpawnedObject is set to false whenever two mixed stacks are combined, i.e., that the Tiller bonus applies if either stack originally had it. Note that this code change will not retroactively fix data in games started pre-v1.4, where the Tiller bonus could be lost depending upon how stacks were combined.
1.4: Changed Wild Plum category from Forage to Fruit in Brazilian-Portugese, Chinese, German, Japanese, Spanish, and Russian. Fixed how Tiller bonus applies when foraged and non-foraged fruit are combined in one stack.